summaryrefslogtreecommitdiff
path: root/src/common/rtdb.c
blob: 5467244662e4ae76240a59befbefdfe0af0fe046 (plain)
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
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
/*
 * Routines to read a data base of run-time information.
 */
#include "../h/gsupport.h"
#include "../h/version.h"
#include "icontype.h"

/*
 * GetInt - the next thing in the data base is an integer. Get it.
 */
#define GetInt(n, c)\
   n = 0;\
   while (isdigit(c)) {\
      n = n * 10 + (c - '0');\
      c = getc(db);\
      }

/*
 * SkipWhSp - skip white space characters in the data base.
 */
#define SkipWhSp(c)\
   while (isspace(c)) {\
      if (c == '\n')\
         ++dbline;\
      c = getc(db);\
      }

/*
 * prototypes for static functions.
 */
static int            cmp_1_pre  (int p1, int p2);
static struct il_code *db_abstr  (void);
static void         db_case   (struct il_code *il, int num_cases);
static void         db_err3   (int fatal,char *s1,char *s2,char *s3);
static int             db_icntyp (void);
static struct il_c    *db_ilc    (void);
static struct il_c    *db_ilcret (int il_c_type);
static struct il_code *db_inlin  (void);
static struct il_code *db_ilvar  (void);
static int             db_rtflg  (void);
static int             db_tndtyp (void);
static struct il_c    *new_ilc   (int il_c_type);
static void          quoted   (int delim);

extern char *progname;   /* name of program using this module */

static char *dbname;           /* data base name */
static FILE *db;               /* data base  file */
static int dbline;             /* line number current position in data base */
static struct str_buf db_sbuf; /* string buffer */
static int *type_map;          /* map data base type codes to internal ones */
static int *compnt_map;        /* map data base component codes to internal */

/*
 * opendb - open data base and do other house keeping.
 */
int db_open(s, lrgintflg)
char *s;
char **lrgintflg;
   {
   char *msg_buf;
   char *id;
   int i, n;
   register int c;
   static int first_time = 1;

   if (first_time) {
      first_time = 0;
      init_sbuf(&db_sbuf);
      }
   dbname = s;
   dbline = 0;
   *lrgintflg = NULL;
   db = fopen(dbname, "rb");
   if (db == NULL)
      return 0;
   ++dbline;

   /*
    * Make sure the version number in the data base is what is expected.
    */
   s = db_string();
   if (strcmp(s, DVersion) != 0) {
      msg_buf = alloc(35 + strlen(s) + strlen(progname) + strlen(DVersion));
      sprintf(msg_buf, "found version %s, %s requires version %s",
           s, progname, DVersion);
      db_err1(1, msg_buf);
      }

   *lrgintflg = db_string();  /* large integer flag */

   /*
    * Create tables for mapping type codes and type component codes in
    *  the data base to those compiled into this program. The codes may
    *  be different if types have been added to the program since the
    *  data base was created.
    */
   type_map = alloc(num_typs * sizeof(int));
   db_chstr("", "types");   /* verify section header */
   c = getc(db);
   SkipWhSp(c)
   while (c == 'T') {
      c = getc(db);
      if (!isdigit(c))
         db_err1(1, "expected type code");
      GetInt(n, c)
      if (n >= num_typs)
         db_err1(1, "data base inconsistant with program, rebuild data base");
      SkipWhSp(c)
      if (c != ':')
         db_err1(1, "expected ':'");
      id = db_string();
      for (i = 0; strcmp(id, icontypes[i].id) != 0; ++i)
         if (i >= num_typs)
            db_err2(1, "unknown type:", id);
      type_map[n] = i;
      c = getc(db);
      SkipWhSp(c)
      }
   db_chstr("", "endsect");

   compnt_map = alloc(num_cmpnts * sizeof(int));
   db_chstr("", "components");   /* verify section header */
   c = getc(db);
   SkipWhSp(c)
   while (c == 'C') {
      c = getc(db);
      if (!isdigit(c))
         db_err1(1, "expected type component code");
      GetInt(n, c)
      if (n >= num_cmpnts)
         db_err1(1, "data base inconsistant with program, rebuild data base");
      SkipWhSp(c)
      if (c != ':')
         db_err1(1, "expected ':'");
      id = db_string();
      for (i = 0; strcmp(id, typecompnt[i].id) != 0; ++i)
         if (i >= num_cmpnts)
            db_err2(1, "unknown type component:", id);
      compnt_map[n] = i;
      c = getc(db);
      SkipWhSp(c)
      }
   db_chstr("", "endsect");

   return 1;
   }

/*
 * db_close - close data base.
 */
void db_close()
   {
   if (fclose(db) != 0)
      db_err2(0, "cannot close", dbname);
   }

/*
 * db_string - get a white-space delimited string from the data base.
 */
char *db_string()
   {
   register int c;

   /*
    * Look for the start of the string; '$' starts a special indicator.
    *  Copy characters into string buffer until white space is found.
    */
   c = getc(db);
   SkipWhSp(c);
   if (c == EOF)
      db_err1(1, "unexpected EOF");
   if (c == '$')
      return NULL;
   while (!isspace(c) && c != EOF) {
      AppChar(db_sbuf, c);
      c = getc(db);
      }
   if (c == '\n')
      ++dbline;
   return str_install(&db_sbuf); /* put string in string table */
   }

/*
 * db_impl - read basic header information for an operation into a structure
 *   and return it.
 */
struct implement *db_impl(oper_typ)
int oper_typ;
   {
   register struct implement *ip;
   register int c;
   int i;
   char *name;
   long n;

   /*
    * Get operation name.
    */
   if ((name = db_string()) == NULL)
      return NULL;

   /*
    * Create an internal structure to hold the data base entry.
    */
   ip = NewStruct(implement);
   ip->blink = NULL;
   ip->iconc_flgs = 0;         /* reserved for internal use by compiler */
   ip->oper_typ = oper_typ;
   ip->name = name;
   ip->op = NULL;

   /*
    * Get the function name prefix assigned to this operation.
    */
   c = getc(db);
   SkipWhSp(c)
   if (isalpha(c) || isdigit(c))
      ip->prefix[0] = c;
   else
     db_err2(1, "invalid prefix for", ip->name);
   c = getc(db);
   if (isalpha(c) || isdigit(c))
      ip->prefix[1] = c;
   else
     db_err2(1, "invalid prefix for", ip->name);

   /*
    * Get the number of parameters.
    */
   c = getc(db);
   SkipWhSp(c)
   if (!isdigit(c))
     db_err2(1, "number of parameters missing for", ip->name);
   GetInt(n, c)
   ip->nargs = n;

   /*
    * Get the flags that indicate whether each parameter requires a dereferenced
    *  and/or undereferenced value, and whether the last parameter represents
    *  the end of a varargs list. Store the flags in an array.
    */
   if (n == 0)
      ip->arg_flgs = NULL;
   else
      ip->arg_flgs = alloc(n * sizeof(int));
   if (c != '(')
      db_err2(1, "parameter flags missing for", ip->name);
   c = getc(db);
   for (i = 0; i < n; ++i) {
      if (c == ',' || c == ')')
         db_err2(1, "parameter flag missing for", ip->name);
      ip->arg_flgs[i] = 0;
      while (c != ',' && c != ')') {
          switch (c) {
             case 'u':
                ip->arg_flgs[i] |= RtParm;
                break;
             case 'd':
                ip->arg_flgs[i] |= DrfPrm;
                break;
             case 'v':
                ip->arg_flgs[i] |= VarPrm;
                break;
             default:
                db_err2(1, "invalid parameter flag for", ip->name);
             }
         c = getc(db);
         }
      if (c == ',')
         c = getc(db);
      }
   if (c != ')')
     db_err2(1, "invalid parameter flag list for", ip->name);

   /*
    * Get the result sequence indicator for the operation.
    */
   c = getc(db);
   SkipWhSp(c)
   if (c != '{')
     db_err2(1, "result sequence missing for", ip->name);
   c = getc(db);
   ip->resume = 0;
   if (c == '}') {
      ip->min_result = NoRsltSeq;
      ip->max_result = NoRsltSeq;
      }
   else {
      if (!isdigit(c))
        db_err2(1, "invalid result sequence for", ip->name);
      GetInt(n, c)
      ip->min_result = n;
      if (c != ',')
        db_err2(1, "invalid result sequence for", ip->name);
      c = getc(db);
      if (c == '*') {
         ip->max_result = UnbndSeq;
         c = getc(db);
         }
      else if (isdigit(c)) {
         GetInt(n, c)
         ip->max_result = n;
         }
      else
        db_err2(1, "invalid result sequence for", ip->name);
      if (c == '+') {
         ip->resume = 1;
         c = getc(db);
         }
      if (c != '}')
        db_err2(1, "invalid result sequence for", ip->name);
      }

   /*
    * Get the flag indicating whether the operation contains returns, fails,
    *  or suspends.
    */
   ip->ret_flag = db_rtflg();

   /*
    * Get the t/f flag that indicates whether the operation explicitly
    *  uses the 'result' location.
    */
   c = getc(db);
   SkipWhSp(c)
   switch (c) {
      case 't':
         ip->use_rslt = 1;
         break;
      case 'f':
         ip->use_rslt = 0;
         break;
      default:
         db_err2(1, "invalid 'result' use indicator for", ip->name);
         }
   return ip;
   }

/*
 * db_code - read the RTL code for the body of an operation.
 */
void db_code(ip)
struct implement *ip;
   {
   register int c;
   char *s;
   word n;
   int var_type;
   int i;

   /*
    * read the descriptive string.
    */
   c = getc(db);
   SkipWhSp(c)
   if (c != '"')
      db_err1(1, "operation description expected");
   for (c = getc(db); c != '"' && c != '\n' && c != EOF; c = getc(db)) {
      if (c == '\\') {
         AppChar(db_sbuf, c);
         c = getc(db);
         }
      AppChar(db_sbuf, c);
      }
   if (c != '"')
      db_err1(1, "expected '\"'");
   ip->comment = str_install(&db_sbuf);

   /*
    * Get the number of tended variables in the declare clause.
    */
   c = getc(db);
   SkipWhSp(c)
   GetInt(n, c)
   ip->ntnds = n;

   /*
    * Read information about the tended variables into an array.
    */
   if (n == 0)
      ip->tnds = NULL;
   else
      ip->tnds = alloc(n * sizeof(struct tend_var));
   for (i = 0; i < n; ++i) {
      var_type = db_tndtyp();  /* type of tended declaration */
      ip->tnds[i].var_type = var_type;
      ip->tnds[i].blk_name = NULL;
      if (var_type == TndBlk) {
         /*
          * Tended block pointer declarations include a block type or '*' to
          *  indicate 'union block *'.
          */
         s = db_string();
         if (s == NULL)
            db_err1(1, "block name expected");
         if (*s != '*')
            ip->tnds[i].blk_name = s;
         }
      ip->tnds[i].init = db_ilc();  /* C code for declaration initializer */
      }

   /*
    * Get the number of non-tended variables in the declare clause.
    */
   c = getc(db);
   SkipWhSp(c)
   GetInt(n, c)
   ip->nvars = n;

   /*
    * Get each non-tended declaration and store it in an array.
    */
   if (n == 0)
      ip->vars = NULL;
   else
      ip->vars = alloc(n * sizeof(struct ord_var));
   for (i = 0; i < n; ++i) {
      s = db_string();             /* variable name */
      if (s == NULL)
         db_err1(1, "variable name expected");
      ip->vars[i].name = s;
      ip->vars[i].dcl = db_ilc();  /* full declaration including name */
      }

   /*
    * Get the executable RTL code.
    */
   ip->in_line = db_inlin();

   /*
    * We should be at the end of the operation.
    */
   c = getc(db);
   SkipWhSp(c)
   if (c != '$')
      db_err1(1, "expected $end");
   }

/*
 * db_inlin - read in the in-line code (executable RTL code) for an operation.
 */
static struct il_code *db_inlin()
   {
   struct il_code *il = NULL;
   register int c;
   int i;
   int indx;
   int fall_thru;
   int n, n1;

   /*
    * The following nested switch statements act as a trie for recognizing
    *  the prefix form of RTL code in the data base.
    */
   c = getc(db);
   SkipWhSp(c)
   switch (c) {
      case 'a':
         switch (getc(db)) {
            case 'b': {
               db_chstr("ab", "str");
               il = new_il(IL_Abstr, 2);        /* abstract type computation */
               il->u[0].fld = db_abstr();       /* side effects */
               il->u[1].fld = db_abstr();       /* return type */
               break;
               }
            case 'c': {
               db_chstr("ac", "ase");
               il = new_il(IL_Acase, 5);        /* arith_case */
               il->u[0].fld = db_ilvar();       /* first variable */
               il->u[1].fld = db_ilvar();       /* second variable */
               il->u[2].fld = db_inlin();       /* C_integer action */
               il->u[3].fld = db_inlin();       /* integer action */
               il->u[4].fld = db_inlin();       /* C_double action */
               break;
               }
            default:
               db_err1(1, "expected abstr or acase");
            }
         break;

      case 'b':
         db_chstr("b", "lock");
         c = getc(db);
         SkipWhSp(c)
         if (c == 't')
            fall_thru = 1;
         else
            fall_thru = 0;
         c = getc(db);
         SkipWhSp(c)
         GetInt(n, c)
         il = new_il(IL_Block, 3 + n);    /* block of in-line C code */
         il->u[0].n = fall_thru;
         il->u[1].n = n;                  /* number of local tended */
         for (i = 2; i - 2 < n; ++i)
             il->u[i].n = db_tndtyp();    /* tended declaration */
         il->u[i].c_cd = db_ilc();        /* C code */
         break;

      case 'c':
         switch (getc(db)) {
            case 'a': {
               char prfx3;
               int ret_val = 0;
               int ret_flag;
               int rslt = 0;
               int num_sbuf;
               int num_cbuf;

               db_chstr("ca", "ll");
               /*
                * Call to body function. Get the letter used as the 3rd
                *  character of the function prefix.
                */
               c = getc(db);
               SkipWhSp(c)
               prfx3 = c;

               /*
                * Determine what the body function returns directly.
                */
               c = getc(db);
               SkipWhSp(c)
               switch (c) {
                  case 'i':
                     ret_val = RetInt;    /* returns C integer */
                     break;
                  case 'd':
                     ret_val = RetDbl;    /* returns C double */
                     break;
                  case 'n':
                     ret_val = RetNoVal;  /* returns nothing directly */
                     break;
                  case 's':
                     ret_val = RetSig;    /* returns a signal */
                     break;
                  default:
                     db_err1(1, "invalid indicator for type of return value");
                  }

              /*
               * Get the return/suspend/fail/fall-through flag.
               */
               c = getc(db);
               ret_flag = db_rtflg();

               /*
                * Get the flag indicating whether the body function expects
                *  to have an explicit result location passed to it.
                */
               c = getc(db);
               SkipWhSp(c)
               switch (c) {
                  case 't':
                     rslt = 1;
                     break;
                  case 'f':
                     rslt = 0;
                     break;
                  default:
                     db_err1(1, "t or f expected");
                  }

               c = getc(db);
               SkipWhSp(c)
               GetInt(num_sbuf, c)  /* number of cset buffers */
               c = getc(db);
               SkipWhSp(c)
               GetInt(num_cbuf, c)  /* number of string buffers */
               c = getc(db);
               SkipWhSp(c)
               GetInt(n, c)         /* num args */

               il = new_il(IL_Call, 8 + n * 2);
               il->u[0].n = 0;      /* reserved for internal use by compiler */
               il->u[1].n = prfx3;
               il->u[2].n = ret_val;
               il->u[3].n = ret_flag;
               il->u[4].n = rslt;
               il->u[5].n = num_sbuf;
               il->u[6].n = num_cbuf;
               il->u[7].n = n;
               indx = 8;

               /*
                * get the prototype parameter declarations and actual arguments.
                */
               n *= 2;
               while (n--)
                  il->u[indx++].c_cd = db_ilc();
               }
               break;

            case 'n':
               if (getc(db) != 'v')
                  db_err1(1, "expected cnv1 or cnv2");
               switch (getc(db)) {
                  case '1':
                     il = new_il(IL_Cnv1, 2);
                     il->u[0].n = db_icntyp();      /* type code */
                     il->u[1].fld = db_ilvar();     /* source */
                     break;
                  case '2':
                     il = new_il(IL_Cnv2, 3);
                     il->u[0].n = db_icntyp();      /* type code */
                     il->u[1].fld = db_ilvar();     /* source */
                     il->u[2].c_cd = db_ilc();      /* destination */
                     break;
                  default:
                     db_err1(1, "expected cnv1 or cnv2");
                  }
               break;

            case 'o':
               db_chstr("co", "nst");
               il = new_il(IL_Const, 2);     /* constant keyword */
               il->u[0].n = db_icntyp();     /* type code */
               c = getc(db);
               SkipWhSp(c)
               if (c == '"' || c == '\'') {
                  quoted(c);
                  c = getc(db);              /* quoted literal without quotes */
                  }
               else
                  while (c != EOF && !isspace(c)) {
                     AppChar(db_sbuf, c);
                     c = getc(db);
                     }
               il->u[1].s = str_install(&db_sbuf); /* non-quoted values */
               break;

            default:
               db_err1(1, "expected call, const, cnv1, or cnv2");
            }
         break;

      case 'd':
         if (getc(db) != 'e' || getc(db) != 'f')
            db_err1(1, "expected def1 or def2");
         switch (getc(db)) {
            case '1':
               il = new_il(IL_Def1, 3);       /* defaulting, no dest. field */
               il->u[0].n = db_icntyp();      /* type code */
               il->u[1].fld = db_ilvar();     /* source */
               il->u[2].c_cd = db_ilc();      /* default value */
               break;
            case '2':
               il = new_il(IL_Def2, 4);       /* defaulting, with dest. field */
               il->u[0].n = db_icntyp();      /* type code */
               il->u[1].fld = db_ilvar();     /* source */
               il->u[2].c_cd = db_ilc();      /* default value */
               il->u[3].c_cd = db_ilc();      /* destination */
               break;
            default:
               db_err1(1, "expected dflt1 or dflt2");
            }
         break;

      case 'r':
         if (getc(db) != 'u' || getc(db) != 'n' || getc(db) != 'e' ||
            getc(db) != 'r' || getc(db) != 'r')
            db_err1(1, "expected runerr1 or runerr2");
         switch (getc(db)) {
            case '1':
               il = new_il(IL_Err1, 1);       /* runerr, no offending value */
               c = getc(db);
               SkipWhSp(c)
               GetInt(n, c)
               il->u[0].n = n;                /* error number */
               break;
            case '2':
               il = new_il(IL_Err2, 2);       /* runerr, with offending value */
               c = getc(db);
               SkipWhSp(c)
               GetInt(n, c)
               il->u[0].n = n;                /* error number */
               il->u[1].fld = db_ilvar();     /* variable */
               break;
            default:
               db_err1(1, "expected runerr1 or runerr2");
            }
         break;

      case 'i':
         switch (getc(db)) {
            case 'f':
               switch (getc(db)) {
                  case '1':
                     il = new_il(IL_If1, 2);    /* if-then */
                     il->u[0].fld = db_inlin(); /* condition */
                     il->u[1].fld = db_inlin(); /* then clause */
                     break;
                  case '2':
                     il = new_il(IL_If2, 3);     /* if-then-else */
                     il->u[0].fld = db_inlin(); /* condition */
                     il->u[1].fld = db_inlin(); /* then clause */
                     il->u[2].fld = db_inlin(); /* else clause */
                     break;
                  default:
                     db_err1(1, "expected if1 or if2");
                  }
               break;
            case 's':
               il = new_il(IL_Is, 2);         /* type check */
               il->u[0].n = db_icntyp();      /* type code */
               il->u[1].fld = db_ilvar();     /* variable */
               break;
            default:
               db_err1(1, "expected if1, if2, or is");
            }
         break;

      case 'l':
         switch (getc(db)) {
            case 'c':
               db_chstr("lc", "ase");
               c = getc(db);
               SkipWhSp(c)
               GetInt(n, c)
               il = new_il(IL_Lcase, 2 + 2 * n); /* length case */
               il->u[0].n = n;                   /* number of cases */
               indx = 1;
               while (n--) {
                  c = getc(db);
                  SkipWhSp(c)
                  GetInt(n1, c)
                  il->u[indx++].n = n1;           /* selection number */
                  il->u[indx++].fld = db_inlin(); /* action */
                  }
               il->u[indx].fld = db_inlin();      /* default */
               break;

            case 's':
               if (getc(db) != 't')
                  db_err1(1, "expected lst");
               il = new_il(IL_Lst, 2);            /* sequence of code parts */
               il->u[0].fld = db_inlin();         /* 1st part */
               il->u[1].fld = db_inlin();         /* 2nd part */
               break;

            default:
               db_err1(1, "expected lcase or lst");
            }
         break;

      case 'n':
         db_chstr("n", "il");
         il = NULL;
         break;

      case 't': {
         struct il_code *var;

         if (getc(db) != 'c' || getc(db) != 'a' || getc(db) != 's' ||
            getc(db) != 'e')
               db_err1(1, "expected tcase1 or tcase2");
         switch (getc(db)) {
            case '1':
               var = db_ilvar();
               c = getc(db);
               SkipWhSp(c)
               GetInt(n, c)
               il = new_il(IL_Tcase1, 3 * n + 2); /* type case, no default */
               il->u[0].fld = var;                /* variable */
               db_case(il, n);                    /* get cases */
               break;

            case '2':
               var = db_ilvar();
               c = getc(db);
               SkipWhSp(c)
               GetInt(n, c)
               il = new_il(IL_Tcase2, 3 * n + 3);  /* type case, with default */
               il->u[0].fld = var;                 /* variable */
               db_case(il, n);                     /* get cases */
               il->u[3 * n + 2].fld = db_inlin();  /* default */
               break;

            default:
               db_err1(1, "expected tcase1 or tcase2");
            }
         }
         break;

      case '!':
         il = new_il(IL_Bang, 1);                   /* negated condition */
         il->u[0].fld = db_inlin();                 /* condition */
         break;

      case '&':
         if (getc(db) != '&')
            db_err1(1, "expected &&");
         il = new_il(IL_And, 2);                    /* && (conjunction) */
         il->u[0].fld = db_inlin();                 /* 1st operand */
         il->u[1].fld = db_inlin();                 /* 2nd operand */
         break;

      default:
         db_err1(1, "syntax error");
      }
   return il;
   }

/*
 * db_rtflg - get the sequence of 4 [or 5] flags that indicate whether code
 *  for a operation [or body function] returns, fails, suspends, has error
 *  failure, [or execution falls through the code].
 */
static int db_rtflg()
   {
   register int c;
   int ret_flag;

   /*
    * The presence of each flag is indicated by a unique character. Its absence
    *  indicated by '_'.
    */
   ret_flag = 0;
   c = getc(db);
   SkipWhSp(c)
   if (c == 'f')
      ret_flag |= DoesFail;
   else if (c != '_')
     db_err1(1, "invalid return indicator");
   c = getc(db);
   if (c == 'r')
      ret_flag |= DoesRet;
   else if (c != '_')
     db_err1(1, "invalid return indicator");
   c = getc(db);
   if (c == 's')
      ret_flag |= DoesSusp;
   else if (c != '_')
     db_err1(1, "invalid return indicator");
   c = getc(db);
   if (c == 'e')
      ret_flag |= DoesEFail;
   else if (c != '_')
     db_err1(1, "invalid return indicator");
   c = getc(db);
   if (c == 't')
      ret_flag |= DoesFThru;
   else if (c != '_' && c != ' ')
     db_err1(1, "invalid return indicator");
   return ret_flag;
   }

/*
 * db_case - get the cases for a type_case statement from the data base.
 */
static void db_case(il, num_cases)
struct il_code *il;
int num_cases;
   {
   register int c;
   int *typ_vect;
   int i, j;
   int num_types;
   int indx;

   il->u[1].n = num_cases;    /* number of cases */
   indx = 2;
   for (i = 0; i < num_cases; ++i) {
      /*
       * Determine the number of types in this case then store the
       *  type codes in an array.
       */
      c = getc(db);
      SkipWhSp(c)
      GetInt(num_types, c)
      il->u[indx++].n = num_types;
      typ_vect = alloc(num_types * sizeof(int));
      il->u[indx++].vect = typ_vect;
      for (j = 0; j < num_types; ++j)
         typ_vect[j] = db_icntyp();           /* type code */

      il->u[indx++].fld = db_inlin();         /* action */
      }
   }

/*
 * db_ilvar - get a symbol table index for a simple variable or a
 *  subscripted variable from the data base.
 */
static struct il_code *db_ilvar()
   {
   struct il_code *il;
   register int c;
   int n;

   c = getc(db);
   SkipWhSp(c)

   if (isdigit(c)) {
      /*
       * Simple variable: just a symbol table index.
       */
      il = new_il(IL_Var, 1);
      GetInt(n, c)
      il->u[0].n = n;    /* symbol table index */
      }
   else {
      if (c != '[')
         db_err1(1, "expected symbol table index or '['");
      /*
       * Subscripted variable: symbol table index and subscript.
       */
      il = new_il(IL_Subscr, 2);
      c = getc(db);
      SkipWhSp(c);
      GetInt(n, c)
      il->u[0].n = n;    /* symbol table index */
      c = getc(db);
      SkipWhSp(c)
      GetInt(n, c)
      il->u[1].n = n;    /* subscripting index */
      }
   return il;
   }

/*
 * db_abstr - get abstract type computations from the data base.
 */
static struct il_code *db_abstr()
   {
   struct il_code *il = NULL;
   register int c;
   word typcd;
   word indx;
   int n;
   int nargs;

   c = getc(db);
   SkipWhSp(c)
   switch (c) {
      case 'l':
         db_chstr("l", "st");
         il = new_il(IL_Lst, 2);        /* sequence of code parts */
         il->u[0].fld = db_abstr();     /* 1st part */
         il->u[1].fld = db_abstr();     /* 2nd part */
         break;

      case 'n':
         switch (getc(db)) {
            case 'e':
               if (getc(db) != 'w')
                  db_err1(1, "expected new");
               typcd = db_icntyp();
               c = getc(db);
               SkipWhSp(c)
               GetInt(nargs, c)
               il = new_il(IL_New, 2 + nargs);  /* new structure create here */
               il->u[0].n = typcd;              /* type code */
               il->u[1].n = nargs;              /* number of args */
               indx = 2;
               while (nargs--)
                  il->u[indx++].fld = db_abstr(); /* argument for component */
               break;
            case 'i':
               if (getc(db) != 'l')
                  db_err1(1, "expected nil");
               il = NULL;
               break;
            default:
               db_err1(1, "expected new or nil");
            }
       break;

      case 's':
         db_chstr("s", "tore");
         il = new_il(IL_Store, 1);  /* abstract store */
         il->u[0].fld = db_abstr(); /* type to "dereference" */
         break;

      case 't':
         db_chstr("t", "yp");
         il = new_il(IL_IcnTyp, 1);  /* explicit type */
         il->u[0].n = db_icntyp();   /* type code */
         break;

      case 'v':
         db_chstr("v", "artyp");
         il = new_il(IL_VarTyp, 1);        /* variable */
         il->u[0].fld = db_ilvar();        /* symbol table index, etc */
         break;

      case '.':
         il = new_il(IL_Compnt, 2);        /* component access */
         il->u[0].fld = db_abstr();        /* type being accessed */
         c = getc(db);
         SkipWhSp(c)
         switch (c) {
            case 'f':
               il->u[1].n = CM_Fields;
               break;
            case 'C':
               c = getc(db);
               GetInt(n, c)
               il->u[1].n = compnt_map[n];
               break;
            default:
               db_err1(1, "expected component code");
            }
         break;

      case '=':
         il = new_il(IL_TpAsgn, 2);        /* assignment (side effect) */
         il->u[0].fld = db_abstr();        /* left-hand-side */
         il->u[1].fld = db_abstr();        /* right-hand-side */
         break;

      case '+':
         if (getc(db) != '+')
            db_err1(1, "expected ++");
         il = new_il(IL_Union, 2);         /* ++ (union) */
         il->u[0].fld = db_abstr();        /* 1st operand */
         il->u[1].fld = db_abstr();        /* 2nd operand */
         break;

      case '*':
         if (getc(db) != '*')
            db_err1(1, "expected **");
         il = new_il(IL_Inter, 2);         /* ** (intersection) */
         il->u[0].fld = db_abstr();        /* 1st operand */
         il->u[1].fld = db_abstr();        /* 2nd operand */
         break;
      }
   return il;
   }

/*
 * db_ilc - read a piece of in-line C code.
 */
static struct il_c *db_ilc()
   {
   register int c;
   int old_c;
   word n;
   struct il_c *base = NULL;
   struct il_c **nxtp = &base;

   c = getc(db);
   SkipWhSp(c)
   switch (c) {
      case '$':
         /*
          * This had better be the starting $c.
          */
         c = getc(db);
         if (c == 'c') {
            c = getc(db);
            for (;;) {
               SkipWhSp(c)
               if (c == '$') {
                  c = getc(db);
                  switch (c) {
                     case 'c':             /* $cb or $cgoto <cond> <lbl num> */
                        c = getc(db);
                        switch (c) {
                           case 'b':
                              *nxtp = new_ilc(ILC_CBuf);
                              c = getc(db);
                              break;
                           case 'g':
                              db_chstr("$cg", "oto");
                              *nxtp = new_ilc(ILC_CGto);
#ifdef MultiThread
   #undef code
#endif					/* MultiThead */
                              (*nxtp)->code[0] = db_ilc();
                              c = getc(db);
                              SkipWhSp(c);
                              if (!isdigit(c))
                                 db_err1(1, "$cgoto: expected label number");
                              GetInt(n, c);
                              (*nxtp)->n = n;
                              break;
                           default:
                             db_err1(1, "expected $cb or $cgoto");
                           }
                        break;
                     case 'e':
                        c = getc(db);
                        if (c == 'f') {             /* $efail */
                            db_chstr("$ef", "ail");
                            *nxtp = new_ilc(ILC_EFail);
                            c = getc(db);
                            break;
                            }
                        else
                           return base;            /* $e */
                     case 'f':                     /* $fail */
                        db_chstr("$f", "ail");
                        *nxtp = new_ilc(ILC_Fail);
                        c = getc(db);
                        break;
                     case 'g':                     /* $goto <lbl num> */
                        db_chstr("$g", "oto");
                        *nxtp = new_ilc(ILC_Goto);
                        c = getc(db);
                        SkipWhSp(c);
                        if (!isdigit(c))
                           db_err1(1, "$goto: expected label number");
                        GetInt(n, c);
                        (*nxtp)->n = n;
                        break;
                     case 'l':                     /* $lbl <lbl num> */
                        db_chstr("$l", "bl");
                        *nxtp = new_ilc(ILC_Lbl);
                        c = getc(db);
                        SkipWhSp(c);
                        if (!isdigit(c))
                           db_err1(1, "$lbl: expected label number");
                        GetInt(n, c);
                        (*nxtp)->n = n;
                        break;
                     case 'm':                     /* $m[d]<indx> */
                        *nxtp = new_ilc(ILC_Mod);
                        c = getc(db);
                        if (c == 'd') {
                           (*nxtp)->s = "d";
                           c = getc(db);
                           }
                        if (isdigit(c)) {
                           GetInt(n, c);
                           (*nxtp)->n = n;
                           }
                        else if (c == 'r') {
                           (*nxtp)->n = RsltIndx;
                           c = getc(db);
                           }
                        else
                           db_err1(1, "$m: expected symbol table index");
                        break;
                     case 'r':                     /* $r[d]<indx> or $ret ... */
                        c = getc(db);
                        if (isdigit(c) || c == 'd') {
                           *nxtp = new_ilc(ILC_Ref);
                           if (c == 'd') {
                              (*nxtp)->s = "d";
                              c = getc(db);
                              }
                           GetInt(n, c);
                           (*nxtp)->n = n;
                           }
                        else if (c == 'r') {
                           *nxtp = new_ilc(ILC_Ref);
                           (*nxtp)->n = RsltIndx;
                           c = getc(db);
                           }
                        else {
                           if (c != 'e' || getc(db) != 't')
                              db_err1(1, "expected $ret");
                           *nxtp = db_ilcret(ILC_Ret);
                           c = getc(db);
                           }
                        break;
                     case 's':                     /* $sb or $susp ... */
                        c = getc(db);
                        switch (c) {
                           case 'b':
                              *nxtp = new_ilc(ILC_SBuf);
                              c = getc(db);
                              break;
                           case 'u':
                              db_chstr("$su", "sp");
                              *nxtp = db_ilcret(ILC_Susp);
                              c = getc(db);
                              break;
                           default:
                             db_err1(1, "expected $sb or $susp");
                           }
                        break;
                     case 't':                     /* $t[d]<indx> */
                        *nxtp = new_ilc(ILC_Tend);
                        c = getc(db);
                        if (!isdigit(c))
                           db_err1(1, "$t: expected index");
                        GetInt(n, c);
                        (*nxtp)->n = n;
                        break;
                     case '{':
                        *nxtp = new_ilc(ILC_LBrc);
                        c = getc(db);
                        break;
                     case '}':
                        *nxtp = new_ilc(ILC_RBrc);
                        c = getc(db);
                        break;
                     default:
                        db_err1(1, "invalid $ escape in C code");
                     }
                  }
               else {
                  /*
                   * Arbitrary code - gather into a string.
                   */
                  while (c != '$') {
                     if (c == '"' || c == '\'') {
                        quoted(c);
                        c = getc(db);
                        }
                     if (c == '\n')
                        ++dbline;
                     if (c == EOF)
                        db_err1(1, "unexpected EOF in C code");
                     old_c = c;
                     AppChar(db_sbuf, c);
                     c = getc(db);
                     if (old_c == ' ')
                        while (c == ' ')
                           c = getc(db);
                     }
                  *nxtp = new_ilc(ILC_Str);
                  (*nxtp)->s = str_install(&db_sbuf);
                  }
               nxtp = &(*nxtp)->next;
               }
            }
         break;
      case 'n':
         db_chstr("n", "il");
         return NULL;
      }
   db_err1(1, "expected C code of the form $c ... $e or nil");
   /*NOTREACHED*/
   return 0;	/* avoid gcc warning */
   }

/*
 * quoted - get the string for a quoted literal. The first quote mark
 *  has been read.
 */
static void quoted(delim)
int delim;
   {
   register int c;

   AppChar(db_sbuf, delim);
   c = getc(db);
   while (c != delim && c != EOF) {
      if (c == '\\') {
         AppChar(db_sbuf, c);
         c = getc(db);
         if (c == EOF)
            db_err1(1, "unexpected EOF in quoted literal");
         }
      AppChar(db_sbuf, c);
      c = getc(db);
      }
   if (c == EOF)
      db_err1(1, "unexpected EOF in quoted literal");
   AppChar(db_sbuf, c);
   }

/*
 * db_ilcret - get the in-line C code on a return or suspend statement.
 */
static struct il_c *db_ilcret(il_c_type)
int il_c_type;
   {
   struct il_c *ilc;
   int c;
   int n;
   int i;

   ilc = new_ilc(il_c_type);
   ilc->n = db_icntyp();       /* kind of return expression */
   c = getc(db);
   SkipWhSp(c)
   GetInt(n, c)                /* number of arguments in this expression */
   for (i = 0; i < n; ++i)
      ilc->code[i] = db_ilc(); /* an argument to the return expression */
   return ilc;
   }

/*
 * db_tndtyp - get the indication for the type of a tended declaration.
 */
static int db_tndtyp()
   {
   int c;

   c = getc(db);
   SkipWhSp(c)
   switch (c) {
      case 'b':
         db_chstr("b", "lkptr");
         return TndBlk;          /* tended block pointer */
      case 'd':
         db_chstr("d", "esc");
         return TndDesc;         /* tended descriptor */
      case 's':
         db_chstr("s", "tr");
         return TndStr;          /* tended string */
      default:
         db_err1(1, "expected blkptr, desc, or str");
         /* NOTREACHED */
      }
   /* NOTREACHED */
   return 0;	/* avoid gcc warning */
   }

/*
 * db_icntyp - get a type code from the data base.
 */
static int db_icntyp()
   {
   int c;
   int n;

   c = getc(db);
   SkipWhSp(c)
   switch (c) {
      case 'T':
         c = getc(db);
         GetInt(n, c)
         if (n < num_typs)
            return type_map[n];       /* type code from specification system */
         break;
      case 'a':
         return TypAny;               /* a - any type */
      case 'c':
         switch (getc(db)) {
            case 'i':
               return TypCInt;        /* ci - C integer */
            case 'd':
               return TypCDbl;        /* cd - C double */
            case 's':
               return TypCStr;        /* cs - C string */
            }
         break;
      case 'd':
         return RetDesc;              /* d - descriptor on return statement */
      case 'e':
         switch (getc(db)) {
            case 'c':
               if (getc(db) == 'i')
                  return TypECInt;    /* eci - exact C integer */
               break;
            case 'i':
               return TypEInt;        /* ei - exact integer */
            case ' ':
            case '\n':
            case '\t':
                return TypEmpty;      /* e - empty  type */
            }
         break;
      case 'n':
         if (getc(db) == 'v')
            return RetNVar;           /* nv - named variable on return */
         break;
      case 'r':
         if (getc(db) == 'n')
            return RetNone;           /* rn - nothing explicitly returned */
         break;
      case 's':
         if (getc(db) == 'v')
            return RetSVar;           /* sv - structure variable on return */
         break;
      case 't':
         switch (getc(db)) {
            case 'c':
               return TypTCset;       /* tc - temporary cset */
            case 's':
               return TypTStr;        /* ts - temporary string */
            }
         break;
      case 'v':
         return TypVar;               /* v - variable */
      }
   db_err1(1, "invalid type code");
   /* NOTREACHED */
   return 0;	/* avoid gcc warning */
   }

/*
 * new_ilc - allocate a new structure to hold a piece of in-line C code.
 */
static struct il_c *new_ilc(il_c_type)
int il_c_type;
   {
   struct il_c *ilc;
   int i;

   ilc = NewStruct(il_c);
   ilc->next = NULL;
   ilc->il_c_type = il_c_type;
   for (i = 0; i < 3; ++i)
      ilc->code[i] = NULL;
   ilc->n = 0;
   ilc->s = NULL;
   return ilc;
   }

/*
 * new_il - allocate a new structure with "size" fields to hold a piece of
 *   RTL code.
 */
struct il_code *new_il(il_type, size)
int il_type;
int size;
   {
   struct il_code *il;

   il = alloc(sizeof(struct il_code) + (size-1) * sizeof(union il_fld));
   il->il_type = il_type;
   return il;
   }

/*
 * db_dscrd - discard an implementation up to $end, skipping the in-line
 *   RTL code.
 */
void db_dscrd(ip)
struct implement *ip;
   {
   char state;  /* how far along we are at recognizing $end */

   free(ip);
   state = '\0';
   for (;;) {
      switch (getc(db)) {
         case '$':
            state = '$';
            continue;
         case 'e':
            if (state == '$') {
               state = 'e';
               continue;
               }
            break;
         case 'n':
            if (state == 'e') {
               state = 'n';
               continue;
               }
            break;
         case 'd':
            if (state == 'n')
               return;
            break;
         case '\n':
            ++dbline;
            break;
         case EOF:
            db_err1(1, "unexpected EOF");
         }
      state = '\0';
      }
   }

/*
 * db_chstr - we are expecting a specific string. We may already have
 *   read a prefix of it.
 */
void db_chstr(prefix, suffix)
char *prefix;
char *suffix;
   {
   int c;

   c = getc(db);
   SkipWhSp(c)

   for (;;) {
      if (*suffix == '\0' && (isspace(c) || c == EOF)) {
         if (c == '\n')
            ++dbline;
         return;
         }
      else if (*suffix != c)
         break;
      c = getc(db);
      ++suffix;
      }
   db_err3(1, "expected:", prefix, suffix);
   }

/*
 * db_tbl - fill in a hash table of implementation information for the
 *  given section.
 */
int db_tbl(section, tbl)
char *section;
struct implement **tbl;
   {
   struct implement *ip;
   int num_added = 0;
   unsigned hashval;

   /*
    * Get past the section header.
    */
   db_chstr("", section);

   /*
    * Create an entry in the hash table for each entry in the data base.
    *  If multiple data bases are loaded into one hash table, use the
    *  first entry encountered for each operation.
    */
   while ((ip = db_impl(toupper(section[0]))) != NULL) {
      if (db_ilkup(ip->name, tbl) == NULL) {
         db_code(ip);
         hashval = IHasher(ip->name);
         ip->blink = tbl[hashval];
         tbl[hashval] = ip;
         ++num_added;
         db_chstr("", "end");
         }
      else
         db_dscrd(ip);
      }
   db_chstr("", "endsect");
   return num_added;
   }

/*
 * db_ilkup - look up id in a table of implementation information and return
 *  pointer it or NULL if it is not there.
 */
struct implement *db_ilkup(id, tbl)
char *id;
struct implement **tbl;
   {
   register struct implement *ptr;

   ptr = tbl[IHasher(id)];
   while (ptr != NULL && ptr->name != id)
      ptr = ptr->blink;
   return ptr;
   }

/*
 * nxt_pre - assign next prefix. A prefix consists of n characters each from
 *   the range 0-9 and a-z, at least one of which is a digit.
 *
 */
void nxt_pre(pre, nxt, n)
char *pre;
char *nxt;
int n;
   {
   int i, num_dig;

   if (nxt[0] == '\0') {
      fprintf(stderr, "out of unique prefixes\n");
      exit(EXIT_FAILURE);
      }

   /*
    * copy the next prefix into the output string.
    */
   for (i = 0; i < n; ++i)
      pre[i] = nxt[i];

   /*
    * Increment next prefix. First, determine how many digits there are in
    *  the current prefix.
    */
   num_dig = 0;
   for (i = 0; i < n; ++i)
      if (isdigit(nxt[i]))
         ++num_dig;

   for (i = n - 1; i >= 0; --i) {
      switch (nxt[i]) {
         case '9':
            /*
             * If there is at least one other digit, increment to a letter.
             *  Otherwise, start over at zero and continue to the previous
             *  character in the prefix.
             */
            if (num_dig > 1) {
               nxt[i] = 'a';
               return;
               }
            else
               nxt[i] = '0';
            break;

         case 'z':
            /*
             * Start over at zero and continue to previous character in the
             *  prefix.
             */
            nxt[i] = '0';
            ++num_dig;
            break;
         default:
            ++nxt[i];
            return;
         }
      }

   /*
    * Indicate that there are no more prefixes.
    */
   nxt[0] = '\0';
   }

/*
 * cmp_pre - lexically compare 2-character prefixes.
 */
int cmp_pre(pre1, pre2)
char *pre1;
char *pre2;
   {
   int cmp;

   cmp = cmp_1_pre(pre1[0], pre2[0]);
   if (cmp == 0)
      return cmp_1_pre(pre1[1], pre2[1]);
   else
      return cmp;
   }

/*
 * cmp_1_pre - lexically compare 1 character of a prefix.
 */
static int cmp_1_pre(p1, p2)
int p1;
int p2;
   {
   if (isdigit(p1)) {
      if (isdigit(p2))
         return p1 - p2;
      else
         return -1;
      }
    else {
       if (isdigit(p2))
          return 1;
       else
         return p1 - p2;
      }
   }

/*
 * db_err1 - print a data base error message in the form of 1 string.
 */
void db_err1(fatal, s)
int fatal;
char *s;
   {
   if (fatal)
      fprintf(stderr, "error, ");
   else
      fprintf(stderr, "warning, ");
   fprintf(stderr, "data base \"%s\", line %d - %s\n", dbname, dbline, s);
   if (fatal)
      exit(EXIT_FAILURE);
   }

/*
 * db_err2 - print a data base error message in the form of 2 strings.
 */
void db_err2(fatal, s1, s2)
int fatal;
char *s1;
char *s2;
   {
   if (fatal)
      fprintf(stderr, "error, ");
   else
      fprintf(stderr, "warning, ");
   fprintf(stderr, "data base \"%s\", line %d - %s %s\n", dbname, dbline, s1,
      s2);
   if (fatal)
      exit(EXIT_FAILURE);
   }

/*
 * db_err3 - print a data base error message in the form of 3 strings.
 */
static void db_err3(fatal, s1, s2, s3)
int fatal;
char *s1;
char *s2;
char *s3;
   {
   if (fatal)
      fprintf(stderr, "error, ");
   else
      fprintf(stderr, "warning, ");
   fprintf(stderr, "data base \"%s\", line %d - %s %s%s\n", dbname, dbline, s1,
      s2, s3);
   if (fatal)
      exit(EXIT_FAILURE);
   }