summaryrefslogtreecommitdiffstats
path: root/bootstrap_cmds/migcom.tproj/routine.c
blob: f4c077858c37332d470363ac459cbb585b4303e0 (plain) (blame)
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
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
/*
 * Copyright (c) 1999-2018 Apple Inc. All rights reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this
 * file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_LICENSE_HEADER_END@
 */
/*
 * Mach Operating System
 * Copyright (c) 1991,1990 Carnegie Mellon University
 * All Rights Reserved.
 *
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie Mellon
 * the rights to redistribute these changes.
 */

#include "type.h"

#include <mach/message.h>
#include <mach/kern_return.h>
#include "mig_machine.h"
#include "error.h"
#include "alloc.h"
#include "global.h"
#include "routine.h"
#include "write.h"

u_int rtNumber = 0;

static void rtSizeDelta(FILE *file, u_int mask, routine_t *rt);

routine_t *
rtAlloc(void)
{
  routine_t *new;

  new = (routine_t *) calloc(1, sizeof *new);
  if (new == rtNULL)
    fatal("rtAlloc(): %s", strerror(errno));
  new->rtNumber = rtNumber++;
  new->rtName = strNULL;
  new->rtErrorName = strNULL;
  new->rtUserName = strNULL;
  new->rtServerName = strNULL;

  return new;
}

void
rtSkip(void)
{
  rtNumber++;
}

argument_t *
argAlloc(void)
{
  extern void KPD_error(FILE *file, argument_t *arg);

  static argument_t prototype =
  {
    .argName = strNULL,
    .argNext = argNULL,
    .argKind = akNone,
    .argType = itNULL,
    .argKPD_Type = argKPD_NULL,
    .argKPD_Template = (void(*)(FILE *, argument_t *, boolean_t))KPD_error,
    .argKPD_Init = KPD_error,
    .argKPD_Pack = KPD_error,
    .argKPD_Extract = KPD_error,
    .argKPD_TypeCheck = KPD_error,
    .argVarName = strNULL,
    .argMsgField = strNULL,
    .argTTName = strNULL,
    .argPadName = strNULL,
    .argSuffix = strNULL,
    .argFlags = flNone,
    .argDeallocate = d_NO,
    .argCountInOut = FALSE,
    .argRoutine = rtNULL,
    .argCount = argNULL,
    .argSubCount = argNULL,
    .argCInOut = argNULL,
    .argPoly = argNULL,
    .argDealloc = argNULL,
    .argSameCount = argNULL,
    .argParent = argNULL,
    .argMultiplier = 1,
    .argRequestPos = 0,
    .argReplyPos = 0,
    .argByReferenceUser = FALSE,
    .argByReferenceServer = FALSE,
    .argTempOnStack = FALSE
  };
  argument_t *new;

  new = (argument_t *) malloc(sizeof *new);
  if (new == argNULL)
    fatal("argAlloc(): %s", strerror(errno));
  *new = prototype;
  return new;
}

routine_t *
rtMakeRoutine(identifier_t name, argument_t *args)
{
  routine_t *rt = rtAlloc();

  rt->rtName = name;
  rt->rtKind = rkRoutine;
  rt->rtArgs = args;

  return rt;
}

routine_t *
rtMakeSimpleRoutine(identifier_t name, argument_t *args)
{
  routine_t *rt = rtAlloc();

  rt->rtName = name;
  rt->rtKind = rkSimpleRoutine;
  rt->rtArgs = args;

  return rt;
}

char *
rtRoutineKindToStr(routine_kind_t rk)
{
  switch (rk) {

    case rkRoutine:
      return "Routine";

    case rkSimpleRoutine:
      return "SimpleRoutine";

    default:
      fatal("rtRoutineKindToStr(%d): not a routine_kind_t", rk);
      /*NOTREACHED*/
      return strNULL;
  }
}

static void
rtPrintArg(argument_t *arg)
{
  ipc_type_t *it = arg->argType;

  if (!akCheck(arg->argKind, akbUserArg|akbServerArg) ||
      (akIdent(arg->argKind) == akeCount) ||
      (akIdent(arg->argKind) == akeDealloc) ||
      (akIdent(arg->argKind) == akeNdrCode) ||
      (akIdent(arg->argKind) == akePoly))
    return;

  printf("\n\t");

  switch (akIdent(arg->argKind)) {

    case akeRequestPort:
      printf("RequestPort");
      break;

    case akeReplyPort:
      printf("ReplyPort");
      break;

    case akeWaitTime:
      printf("WaitTime");
      break;

    case akeSendTime:
      printf("SendTime");
      break;

    case akeMsgOption:
      printf("MsgOption");
      break;

    case akeMsgSeqno:
      printf("MsgSeqno\t");
      break;

    case akeSecToken:
      printf("SecToken\t");
      break;

    case akeAuditToken:
      printf("AuditToken\t");
      break;

    case akeContextToken:
      printf("ContextToken\t");
      break;

    case akeImplicit:
      printf("Implicit\t");
      break;

    default:
      if (akCheck(arg->argKind, akbRequest)) {
        if (akCheck(arg->argKind, akbSend))
          printf("In");
        else
          printf("(In)");
      }
      if (akCheck(arg->argKind, akbReply)) {
        if (akCheck(arg->argKind, akbReturn))
          printf("Out");
        else
          printf("(Out)");
      }
      printf("\t");
  }

  printf("\t%s: %s", arg->argName, it->itName);

  if (arg->argDeallocate == d_YES)
    printf(", Dealloc");
  else if (arg->argDeallocate == d_MAYBE)
    printf(", Dealloc[]");

  if (arg->argCountInOut)
    printf(", CountInOut");

  if (arg->argFlags & flSameCount)
    printf(", SameCount");

  if (arg->argFlags & flPhysicalCopy)
    printf(", PhysicalCopy");

  if (arg->argFlags & flRetCode)
    printf(", PhysicalCopy");

  if (arg->argFlags & flOverwrite)
    printf(", Overwrite");

  if (arg->argFlags & flAuto)
    printf(", Auto");

  if (arg->argFlags & flConst)
    printf(", Const");
}

void
rtPrintRoutine(routine_t *rt)
{
  argument_t *arg;

  printf("%s (%d) %s(", rtRoutineKindToStr(rt->rtKind), rt->rtNumber, rt->rtName);

  for (arg = rt->rtArgs; arg != argNULL; arg = arg->argNext)
    rtPrintArg(arg);

  printf(")\n");
  printf("\n");
}

/*
 * Determines appropriate value of msg-simple for the message.
 * One version for both In & Out.
 */

static void
rtCheckSimple(argument_t *args, u_int mask, boolean_t *simple)
{
  argument_t *arg;
  boolean_t MustBeComplex = FALSE;

  for (arg = args; arg != argNULL; arg = arg->argNext)
    if (akCheck(arg->argKind, mask)) {
      ipc_type_t *it = arg->argType;

      if (IS_KERN_PROC_DATA(it))
        MustBeComplex = TRUE;
    }

  *simple = !MustBeComplex;
}

static void
rtCheckFit(routine_t *rt, u_int mask, boolean_t *fitp, boolean_t *uselimp, u_int *knownp)
{
  boolean_t uselim = FALSE;
  argument_t *arg;
  u_int size = sizeof(mach_msg_header_t);

  if (!rt->rtSimpleRequest)
    machine_alignment(size,sizeof(mach_msg_body_t));
  for (arg = rt->rtArgs; arg != argNULL; arg = arg->argNext)
    if (akCheck(arg->argKind, mask)) {
      ipc_type_t *it = arg->argType;

      machine_alignment(size, it->itMinTypeSize);
      if (it->itNative)
        uselim = TRUE;
      else if (IS_VARIABLE_SIZED_UNTYPED(it)) {
        machine_alignment(size, it->itTypeSize);
        size += it->itPadSize;
      }
    }
  *knownp = size;
  if (MaxMessSizeOnStack == -1) {
    *fitp = TRUE;
    *uselimp = FALSE;
  }
  else if (size > MaxMessSizeOnStack) {
    *fitp = FALSE;
    *uselimp = FALSE;
  }
  else if (!uselim) {
    *fitp = TRUE;
    *uselimp = FALSE;
  }
  else if (UserTypeLimit == -1) {
    *fitp = FALSE;
    *uselimp = FALSE;
  }
  else if (size + UserTypeLimit > MaxMessSizeOnStack) {
    *fitp = FALSE;
    *uselimp = TRUE;
  }
  else {
    *fitp = TRUE;
    *uselimp = TRUE;
  }
}

static void
rtFindHowMany(routine_t *rt)
{
  argument_t *arg;
  int multiplier = 1;
  boolean_t test;

  for (arg = rt->rtArgs; arg != argNULL; arg = arg->argNext) {
    ipc_type_t *it = arg->argType;

    if (IS_MULTIPLE_KPD(it)) {
      if (!it->itVarArray)
        multiplier = it->itKPD_Number;
      test = !it->itVarArray && !it->itElement->itVarArray;
      it = it->itElement;
    }
    else
      test = !it->itVarArray;

    if (akCheck(arg->argKind, akbSendKPD)) {

      if (it->itInLine)
        rt->rtCountPortsIn += it->itNumber * multiplier;
      else if (it->itPortType) {
        if (test)
          rt->rtCountOolPortsIn += it->itNumber * multiplier;
      }
      else {
        if (test)
          rt->rtCountOolIn += (it->itNumber * it->itSize + 7)/8 * multiplier;
      }
    }
    if (akCheckAll(arg->argKind, akbReturnKPD)) {
      if (it->itInLine)
        rt->rtCountPortsOut += it->itNumber * multiplier;
      else if (it->itPortType) {
        if (test)
          rt->rtCountOolPortsOut += it->itNumber * multiplier;
      }
      else {
        if (test)
          rt->rtCountOolOut += ((it->itNumber * it->itSize + 7)/8) * multiplier;
      }
    }
  }
}

boolean_t
rtCheckMask(argument_t *args, u_int mask)
{
  argument_t *arg;

  for (arg = args; arg != argNULL; arg = arg->argNext)
    if (akCheckAll(arg->argKind, mask))
      return TRUE;
  return FALSE;
}

boolean_t
rtCheckMaskFunction(argument_t *args, u_int mask, boolean_t (*func)(argument_t *arg))
{
  argument_t *arg;

  for (arg = args; arg != argNULL; arg = arg->argNext)
    if (akCheckAll(arg->argKind, mask))
      if ((*func)(arg))
        return TRUE;
  return FALSE;
}


int
rtCountKPDs(argument_t *args, u_int mask)
{
  argument_t *arg;
  int count = 0;

  for (arg = args; arg != argNULL; arg = arg->argNext)
    if (akCheckAll(arg->argKind, mask))
      count += arg->argType->itKPD_Number;
  return count;
}

int
rtCountFlags(argument_t *args, u_int flag)
{
  argument_t *arg;
  int count = 0;

  for (arg = args; arg != argNULL; arg = arg->argNext)
    if (arg->argFlags & flag)
      count++;
  return count;
}

int
rtCountArgDescriptors(argument_t *args, int *argcount)
{
  argument_t *arg;
  int count = 0;

  if (argcount)
    *argcount = 0;
  for (arg = args; arg != argNULL; arg = arg->argNext)
    if (akCheck(arg->argKind, akbServerArg)) {
      if (RPCFixedArray(arg) ||
          RPCPort(arg) ||
          RPCVariableArray(arg) ||
          RPCPortArray(arg)) {
        count++;
        if (argcount)
          (*argcount)++;
      }
      else {
        if (argcount) {
          if (arg->argType->itStruct && arg->argType->itNumber &&
              (arg->argType->itSize >= 32))
            *argcount += arg->argType->itNumber * (arg->argType->itSize / 32);
          else
            (*argcount)++;
        }
      }
    }
  return count;
}

int
rtCountMask(argument_t *args, u_int mask)
{
  argument_t *arg;
  int count = 0;

  for (arg = args; arg != argNULL; arg = arg->argNext)
    if (akCheckAll(arg->argKind, mask))
      count++;
  return count;
}

/* arg->argType may be NULL in this function */

static void
rtDefaultArgKind(routine_t *rt, argument_t *arg)
{
  if ((arg->argKind == akNone) && (rt->rtRequestPort == argNULL))
    arg->argKind = akRequestPort;

  if (arg->argKind == akNone)
    arg->argKind = akIn;
}

/*
 * Initializes arg->argDeallocate,
 * arg->argCountInOut from arg->argFlags
 * and perform consistency check over the
 * flags.
 */

static ipc_flags_t
rtProcessDeallocFlag(ipc_type_t *it, ipc_flags_t flags, arg_kind_t kind, dealloc_t *what, string_t name)
{

  /* only one of flDealloc, flNotDealloc, flMaybeDealloc */

  if (flags & flMaybeDealloc) {
    if (flags & (flDealloc|flNotDealloc)) {
      warn("%s: Dealloc and NotDealloc ignored with Dealloc[]", name);
      flags &= ~(flDealloc|flNotDealloc);
    }
  }

  if ((flags&(flDealloc|flNotDealloc)) == (flDealloc|flNotDealloc)) {
    warn("%s: Dealloc and NotDealloc cancel out", name);
    flags &= ~(flDealloc|flNotDealloc);
  }

  if (((IsKernelServer && akCheck(kind, akbReturn)) ||
       (IsKernelUser && akCheck(kind, akbSend))) &&
      (flags & flDealloc)) {
    /*
     *  For a KernelServer interface and an Out argument,
     *  or a KernelUser interface and an In argument,
     *  we avoid a possible spurious warning about the deallocate bit.
     *  For compatibility with Mach 2.5, the deallocate bit
     *  may need to be enabled on some inline arguments.
     */

    *what= d_YES;
  }
  else if (flags & (flMaybeDealloc|flDealloc)) {
    /* only give semantic warnings if the user specified something */
    if (it->itInLine && !it->itPortType) {
      warn("%s: Dealloc is ignored: it is meaningless  for that type of argument", name);
      flags &= ~(flMaybeDealloc|flDealloc);
    }
    else
      *what = (flags & flMaybeDealloc) ? d_MAYBE : d_YES;
  }
  return flags;
}

static void
rtProcessSameCountFlag(argument_t *arg)
{
  ipc_type_t *it = arg->argType;
  ipc_flags_t flags = arg->argFlags;
  string_t name = arg->argVarName;
  static argument_t *old_arg;

  if (flags & flSameCount)  {
    if (!it->itVarArray) {
      warn("%s: SameCount is ignored - the argument is not variable", name);
      flags &= ~flSameCount;
    }
    if (old_arg) {
      if (old_arg->argParent)
        old_arg = old_arg->argParent;
      if (old_arg->argSameCount)
        old_arg = old_arg->argSameCount;

      if (!old_arg->argType->itVarArray) {
        warn("%s: SameCount is ignored - adjacent argument is not variable", name);
        flags &= ~flSameCount;
      }

#define SAMECOUNT_MASK akeBITS|akbSend|akbReturn|akbRequest|akbReply|akbUserArg|akbServerArg
      if (akCheck(old_arg->argKind, SAMECOUNT_MASK) !=
          akCheck(arg->argKind, SAMECOUNT_MASK) ||
          old_arg->argCountInOut != arg->argCountInOut) {
        warn("%s: SameCount is ignored - inconsistencies with the adjacent argument\n", name);
        flags &= ~flSameCount;
      }
      arg->argSameCount = old_arg;
    }
    arg->argFlags = flags;
  }
  old_arg = arg;
}

static ipc_flags_t
rtProcessCountInOutFlag(ipc_type_t *it, ipc_flags_t flags, arg_kind_t kind, boolean_t *what, string_t name)
{
  if (flags & flCountInOut) {
    if (!akCheck(kind, akbReply)) {
      warn("%s: CountInOut is ignored: argument must be Out\n", name);
      flags &= ~flCountInOut;
    }
    else if (!it->itVarArray || !it->itInLine) {
      warn("%s: CountInOut is ignored: argument isn't variable or in-line\n", name);
      flags &= ~flCountInOut;
    }
    else
      *what = TRUE;
  }
  return flags;
}

static ipc_flags_t
rtProcessPhysicalCopyFlag(ipc_type_t *it, ipc_flags_t flags, arg_kind_t kind, string_t name)
{
  if (flags & flPhysicalCopy) {
    if (it->itInLine) {
      warn("%s: PhysicalCopy is ignored, argument copied inline anyway", name);
      flags &= ~flPhysicalCopy;
    }
    if (it->itPortType) {
      warn("%s: PhysicalCopy is ignored, it does not apply to ports and array of ports", name);
      flags &= ~flPhysicalCopy;
    }
  }
  return flags;
}

static void
rtProcessRetCodeFlag(argument_t *thisarg)
{
  ipc_type_t *it = thisarg->argType;
  ipc_flags_t flags = thisarg->argFlags;
  string_t name = thisarg->argVarName;
  routine_t *thisrout = thisarg->argRoutine;

  if (flags & flRetCode) {
    if (!it->itInLine || !it->itStruct ||
        it->itSize != 32 || it->itNumber != 1) {
      warn("%s: RetCode is ignored - the type doesn't match a MIG RetCode", name);
      flags &= ~flRetCode;
    }
    else if (thisrout->rtKind != rkSimpleRoutine) {
      fatal("%s: RetCode is allowed only for SimpleRoutines", name);
    }
    else if (thisrout->rtRetCArg != argNULL) {
      warn("%s: RetCode is ignored - only one argument can be flagged as RetCode", name);
      flags &= ~flRetCode;
    }
    else {
      thisrout->rtRetCArg = thisarg;
    }
    thisarg->argFlags = flags;
  }
}

static ipc_flags_t
rtProcessOverwriteFlag(ipc_type_t *it, ipc_flags_t flags, arg_kind_t kind, string_t name)
{
  if (flags & flOverwrite)
    if (it->itInLine || it->itMigInLine ||
        /* among In, Out, InOut, we want only the Out! */
        !akCheck(kind, akbReturn) || akCheck(kind, akbSend)) {
      warn("%s: Overwrite is ignored - it must be Out AND Ool!", name);
      flags &= ~flOverwrite;
    }
  return flags;
}

static void
rtDetectKPDArg(argument_t *arg)
{
  ipc_type_t *it = arg->argType;
  char *string;

  if  (IS_KERN_PROC_DATA(it)) {
    if (akCheck(arg->argKind, akbSendBody)) {
      arg->argKind = akRemFeature(arg->argKind, akbSendBody);
      arg->argKind = akAddFeature(arg->argKind, akbSendKPD);
    }
    if (akCheck(arg->argKind, akbReturnBody)) {
      arg->argKind = akRemFeature(arg->argKind, akbReturnBody);
      arg->argKind = akAddFeature(arg->argKind, akbReturnKPD);
    }
    if (it->itInLine) {
      string = "mach_msg_port_descriptor_t";
      arg->argKPD_Type = MACH_MSG_PORT_DESCRIPTOR;
    }
    else if (it->itPortType) {
      string = "mach_msg_ool_ports_descriptor_t";
      arg->argKPD_Type = MACH_MSG_OOL_PORTS_DESCRIPTOR;
    }
    else {
      string = "mach_msg_ool_descriptor_t";
      arg->argKPD_Type = MACH_MSG_OOL_DESCRIPTOR;
    }
    it->itKPDType = string;
  }
}

static void
rtAugmentArgKind(argument_t *arg)
{
  ipc_type_t *it = arg->argType;

  /* akbVariable means variable-sized inline */

  if (IS_VARIABLE_SIZED_UNTYPED(it)) {
    if (akCheckAll(arg->argKind, akbRequest|akbReply))
      error("%s: Inline variable-sized arguments can't be InOut", arg->argName);
    arg->argKind = akAddFeature(arg->argKind, akbVariable);
  }
  if (IS_OPTIONAL_NATIVE(it))
    arg->argKind = akAddFeature(arg->argKind, akbVariable);

  /*
   * Need to use a local variable in the following cases:
   *  1) There is a translate-out function & the argument is being
   *     returned.  We need to translate it before it hits the message.
   *  2) There is a translate-in function & the argument is
   *     sent and returned.  We need a local variable for its address.
   *  3) There is a destructor function, which will be used
   *     (SendRcv and not ReturnSnd), and there is a translate-in
   *     function whose value must be saved for the destructor.
   *  4) This is Complex KPD (array of KPD), and as such it has to
   *     be copied to a local array in input and output
   *  5) Both poly and dealloc generate warnings compile time, because
   *     we attempt to take address of bit-field structure member
   */

  if (
      ((it->itOutTrans != strNULL) && akCheck(arg->argKind, akbReturnSnd)) ||
      ((it->itInTrans != strNULL) && akCheckAll(arg->argKind, akbSendRcv|akbReturnSnd)) ||
      ((it->itDestructor != strNULL) && akCheck(arg->argKind, akbSendRcv) && !akCheck(arg->argKind, akbReturnSnd) && (it->itInTrans != strNULL)) ||
      (IS_MULTIPLE_KPD(it)) ||
      ((akIdent(arg->argKind) == akePoly) && akCheck(arg->argKind, akbReturnSnd))  ||
      ((akIdent(arg->argKind) == akeDealloc) && akCheck(arg->argKind, akbReturnSnd))
     ) {
    arg->argKind = akRemFeature(arg->argKind, akbReplyCopy);
    arg->argKind = akAddFeature(arg->argKind, akbVarNeeded);
  }
}

/*
 * The Suffix allows to handle KPDs as normal data.
 * it is used in InArgMsgField.
 */
static void
rtSuffixExtArg(argument_t *args)
{
  argument_t *arg;
  char *subindex;
  char string[MAX_STR_LEN];

  for (arg = args; arg != argNULL; arg = arg->argNext)  {
    if (akCheck(arg->argKind, akbSendKPD | akbReturnKPD)) {
      if (IS_MULTIPLE_KPD(arg->argType))
        subindex = "[0]";
      else
        subindex = "";
      switch (arg->argKPD_Type) {

        case MACH_MSG_PORT_DESCRIPTOR:
          (void)sprintf(string, "%s.name", subindex);
          break;

        case MACH_MSG_OOL_DESCRIPTOR:
        case MACH_MSG_OOL_PORTS_DESCRIPTOR:
          (void)sprintf(string, "%s.address", subindex);
          break;

        default:
          error("Type of kernel processed data unknown\n");
      }
      arg->argSuffix = strconcat(arg->argMsgField, string);
      /* see above the list of VarNeeded cases */
      /*
       * argCount has been removed from the VarNeeded list,
       * because VarSize arrays have their Count in the untyped
       * section of the message, and because it is not possible
       * to move anything in-line/out-of-line
       */
    }
    else if (akIdent(arg->argKind) == akePoly &&
               akCheck(arg->argParent->argKind, akbSendKPD | akbReturnKPD)) {
      argument_t *par_arg = arg->argParent;

      if (IS_MULTIPLE_KPD(par_arg->argType))
        subindex = "[0]";
      else
        subindex = "";
      switch (par_arg->argKPD_Type) {

        case MACH_MSG_PORT_DESCRIPTOR:
        case MACH_MSG_OOL_PORTS_DESCRIPTOR:
          (void)sprintf(string, "%s.disposition", subindex);
          arg->argSuffix = strconcat(par_arg->argMsgField, string);
          break;

        default:
          error("Type of kernel processed data inconsistent\n");
      }
    }
    else if (akIdent(arg->argKind) == akeDealloc &&
               akCheck(arg->argParent->argKind, akbSendKPD | akbReturnKPD)) {
      argument_t *par_arg = arg->argParent;

      if (IS_MULTIPLE_KPD(par_arg->argType))
        subindex = "[0]";
      else
        subindex = "";
      switch (par_arg->argKPD_Type) {

        case MACH_MSG_OOL_DESCRIPTOR:
        case MACH_MSG_OOL_PORTS_DESCRIPTOR:
          (void)sprintf(string, "%s.deallocate", subindex);
          arg->argSuffix = strconcat(par_arg->argMsgField, string);
          break;

        default:
          error("Type of kernel processed data inconsistent\n");
      }
    }
  }
}

/* arg->argType may be NULL in this function */

static void
rtCheckRoutineArg(routine_t *rt, argument_t *arg)
{
  switch (akIdent(arg->argKind)) {

    case akeRequestPort:
      if (rt->rtRequestPort != argNULL)
        warn("multiple RequestPort args in %s; %s won't be used", rt->rtName, rt->rtRequestPort->argName);
      rt->rtRequestPort = arg;
      break;

    case akeReplyPort:
      if (rt->rtReplyPort != argNULL)
        warn("multiple ReplyPort args in %s; %s won't be used", rt->rtName, rt->rtReplyPort->argName);
      rt->rtReplyPort = arg;
      break;

    case akeWaitTime:
      if (rt->rtWaitTime != argNULL)
        warn("multiple WaitTime/SendTime type args in %s; %s won't be used", rt->rtName, rt->rtWaitTime->argName);
      rt->rtWaitTime = arg;
      break;

    case akeSendTime:
      if (rt->rtWaitTime != argNULL) {
        if (akIdent(rt->rtWaitTime->argKind) == akeWaitTime) {
          warn("SendTime type argument after a WaitTime in %s; SendTime %s won't be used", rt->rtName, arg->argName);
          break;
        } else  {
          warn("multiple SendTime type args in %s; %s won't be used", rt->rtName, rt->rtWaitTime->argName);
        }
      }
      rt->rtWaitTime = arg;
      break;

    case akeMsgOption:
      if (rt->rtMsgOption != argNULL)
        warn("multiple MsgOption args in %s; %s won't be used", rt->rtName, rt->rtMsgOption->argName);
      rt->rtMsgOption = arg;
      break;

    default:
      break;
  }
}

/* arg->argType may be NULL in this function */

static void
rtSetArgDefaults(routine_t *rt, argument_t *arg)
{
  arg->argRoutine = rt;
  if (arg->argVarName == strNULL)
    arg->argVarName = arg->argName;
  if (arg->argMsgField == strNULL)
    switch(akIdent(arg->argKind)) {

      case akeRequestPort:
        arg->argMsgField = "Head.msgh_request_port";
        break;

      case akeReplyPort:
        arg->argMsgField = "Head.msgh_reply_port";
        break;

      case akeNdrCode:
        arg->argMsgField = "NDR";
        break;

      case akeSecToken:
        arg->argMsgField = "msgh_sender";
        break;

      case akeAuditToken:
        arg->argMsgField = "msgh_audit";
        break;

      case akeContextToken:
        arg->argMsgField = "msgh_context";
        break;

      case akeMsgSeqno:
        arg->argMsgField = "msgh_seqno";
        break;

      case akeImplicit:
        /* the field is set directly by Yacc */
        break;

      default:
        arg->argMsgField = arg->argName;
        break;
  }

  if (arg->argTTName == strNULL)
    arg->argTTName = strconcat(arg->argName, "Template");
  if (arg->argPadName == strNULL)
    arg->argPadName = strconcat(arg->argName, "Pad");

  /*
   *  The poly args for the request and reply ports have special defaults,
   *  because their msg-type-name values aren't stored in normal fields.
   */

  if ((rt->rtRequestPort != argNULL) &&
      (rt->rtRequestPort->argPoly == arg) &&
      (arg->argType != itNULL)) {
    arg->argMsgField = "Head.msgh_bits";
    arg->argType->itInTrans = "MACH_MSGH_BITS_REQUEST";
  }

  if ((rt->rtReplyPort != argNULL) &&
      (rt->rtReplyPort->argPoly == arg) &&
      (arg->argType != itNULL)) {
    arg->argMsgField = "Head.msgh_bits";
    arg->argType->itInTrans = "MACH_MSGH_BITS_REPLY";
  }
}

static void
rtAddCountArg(argument_t *arg)
{
  argument_t *count, *master;
  ipc_type_t *it = arg->argType;

  count = argAlloc();

  if (IS_MULTIPLE_KPD(it) && it->itElement->itVarArray) {
    count->argName = strconcat(arg->argName, "Subs");
    count->argType = itMakeSubCountType(it->itKPD_Number, it->itVarArray, arg->argVarName);
    count->argKind = akeSubCount;
    arg->argSubCount = count;
  }
  else {
    count->argName = strconcat(arg->argName, "Cnt");
    count->argType = itMakeCountType();
    count->argKind = akeCount;
    arg->argCount = count;
    if (arg->argParent != argNULL)  {
      /* this is the case where we are at the second level of recursion:
       we want the Parent to access it through argCount */
      arg->argParent->argCount = count;
    }
  }
  master = (arg->argParent != argNULL) ? arg->argParent : arg;
  if (IS_MULTIPLE_KPD(master->argType))
    count->argMultiplier = 1;
  else
    count->argMultiplier = it->itElement->itNumber;
  count->argParent = arg;
  count->argNext = arg->argNext;
  arg->argNext = count;

  if (arg->argType->itString) {
    /* C String gets no Count argument on either side, but are still in the msg */
    count->argKind = akAddFeature(count->argKind, akCheck(arg->argKind, akbSend) ? akbSendRcv : akbReturnRcv);
    count->argVarName = (char *)0;
  }
  else {
    /*
     * Count arguments have to be present on the message body (NDR encoded)
     * akbVariable has to be turned down, has it foul the algorithm
     * for detecting the in-line variable sized arrays
     */
    count->argKind |= akAddFeature(akbUserArg|akbServerArg, (arg->argKind) & ~akeBITS);
    count->argKind = akRemFeature(count->argKind, akbVariable|akbVarNeeded);
    if (IS_VARIABLE_SIZED_UNTYPED(arg->argType))
    /*
     * Count arguments for the above types are explicitly declared
     * BEFORE the variable (with those bits, they would come afterwards)
     */
    count->argKind = akRemFeature(count->argKind, akbRequest|akbReply);
  }
}

static void
rtAddCountInOutArg(argument_t *arg)
{
  argument_t *count;

  /*
   *  The user sees a single count variable.  However, to get the
   *  count passed from user to server for variable-sized inline OUT
   *  arrays, we need two count arguments internally.  This is
   *  because the count value lives in different message fields (and
   *  is scaled differently) in the request and reply messages.
   *
   *  The two variables have the same name to simplify code generation.
   *
   *  This variable has a null argParent field because it has akbRequest.
   *  For example, see rtCheckVariable.
   */

  count = argAlloc();
  count->argName = strconcat(arg->argName, "Cnt");
  count->argType = itMakeCountType();
  count->argParent = argNULL;
  count->argNext = arg->argNext;
  arg->argNext = count;
  (count->argCInOut = arg->argCount)->argCInOut = count;
  count->argKind = akCountInOut;
}

static void
rtAddPolyArg(argument_t *arg)
{
  ipc_type_t *it = arg->argType;
  argument_t *poly;
  arg_kind_t akbsend, akbreturn;

  poly = argAlloc();
  poly->argName = strconcat(arg->argName, "Poly");
  poly->argType = itMakePolyType();
  poly->argParent = arg;
  poly->argNext = arg->argNext;
  arg->argNext = poly;
  arg->argPoly = poly;

  /*
   * akbsend is bits added if the arg is In;
   * akbreturn is bits added if the arg is Out.
   * The mysterious business with KernelServer subsystems:
   * when packing Out arguments, they use OutNames instead
   * of InNames, and the OutName determines if they are poly-in
   * as well as poly-out.
   */

  akbsend = akbSend;
  akbreturn = akbReturn;

  if (it->itInName == MACH_MSG_TYPE_POLYMORPHIC) {
    akbsend |= akbUserArg|akbSendSnd;
    if (!IsKernelServer)
      akbreturn |= akbServerArg|akbReturnSnd;
  }
  if (it->itOutName == MACH_MSG_TYPE_POLYMORPHIC) {
    akbsend |= akbServerArg|akbSendRcv;
    akbreturn |= akbUserArg|akbReturnRcv;
    if (IsKernelServer)
      akbreturn |= akbServerArg|akbReturnSnd;
  }

  poly->argKind = akPoly;
  if (akCheck(arg->argKind, akbSend))
    poly->argKind = akAddFeature(poly->argKind, akCheck(arg->argKind, akbsend));
  if (akCheck(arg->argKind, akbReturn))
    poly->argKind = akAddFeature(poly->argKind, akCheck(arg->argKind, akbreturn));
}

static void
rtAddDeallocArg(argument_t *arg)
{
  argument_t *dealloc;

  dealloc = argAlloc();
  dealloc->argName = strconcat(arg->argName, "Dealloc");
  dealloc->argType = itMakeDeallocType();
  dealloc->argParent = arg;
  dealloc->argNext = arg->argNext;
  arg->argNext = dealloc;
  arg->argDealloc = dealloc;

  /*
   * Dealloc flag can only be associated to KPDs.
   */

  dealloc->argKind = akeDealloc;
  if (akCheck(arg->argKind, akbSend))
    dealloc->argKind = akAddFeature(dealloc->argKind, akCheck(arg->argKind, akbUserArg|akbSend|akbSendSnd));
  if (akCheck(arg->argKind, akbReturn)) {
    dealloc->argKind = akAddFeature(dealloc->argKind, akCheck(arg->argKind, akbServerArg|akbReturn|akbReturnSnd));
    dealloc->argByReferenceServer = TRUE;
  }
}

static void
rtCheckRoutineArgs(routine_t *rt)
{
  argument_t *arg;

  for (arg = rt->rtArgs; arg != argNULL; arg = arg->argNext) {
    ipc_type_t *it = arg->argType;

    rtDefaultArgKind(rt, arg);
    rtCheckRoutineArg(rt, arg);

    /* need to set argTTName before adding implicit args */
    rtSetArgDefaults(rt, arg);

    /* the arg may not have a type (if there was some error in parsing it),
       in which case we don't want to do these steps. */

    if (it != itNULL) {
      arg->argFlags = rtProcessDeallocFlag(it, arg->argFlags, arg->argKind, &arg->argDeallocate, arg->argVarName);
      arg->argFlags = rtProcessCountInOutFlag(it, arg->argFlags, arg->argKind, &arg->argCountInOut, arg->argVarName);
      rtProcessSameCountFlag(arg);
      arg->argFlags = rtProcessPhysicalCopyFlag(it, arg->argFlags, arg->argKind, arg->argVarName);
      rtProcessRetCodeFlag(arg);
      arg->argFlags = rtProcessOverwriteFlag(it, arg->argFlags, arg->argKind, arg->argVarName);
      rtAugmentArgKind(arg);

      /* args added here will get processed in later iterations */
      /* order of args is 'arg poly countinout count dealloc' */

      if (arg->argDeallocate == d_MAYBE)
        rtAddDeallocArg(arg);
      if (it->itVarArray || (IS_MULTIPLE_KPD(it) && it->itElement->itVarArray))
        rtAddCountArg(arg);
      if (arg->argCountInOut)
        rtAddCountInOutArg(arg);
      if ((it->itInName == MACH_MSG_TYPE_POLYMORPHIC) || (it->itOutName == MACH_MSG_TYPE_POLYMORPHIC))
        rtAddPolyArg(arg);
      /*
       * Detects whether the arg has to become part of the
       * Kernel Processed Data section; if yes, define the proper
       * itUserKPDType, itServerKPDType
       */
      rtDetectKPDArg(arg);
    }
  }
}

boolean_t
rtCheckTrailerType(argument_t *arg)
{
  if (akIdent(arg->argKind) == akeSecToken ||
      akIdent(arg->argKind) == akeAuditToken ||
      akIdent(arg->argKind) == akeContextToken )
    itCheckTokenType(arg->argVarName, arg->argType);

  if (akIdent(arg->argKind) == akeMsgSeqno)
    itCheckIntType(arg->argVarName, arg->argType);
  /*
   * if the built-in are not used, we cannot match
   * the type/size of the desciption provided by the user
   * with the one defined in message.h.
   */

  return TRUE;
}

static void
rtCheckArgTypes(routine_t *rt)
{
  if (rt->rtRequestPort == argNULL)
    error("%s %s doesn't have a server port argument", rtRoutineKindToStr(rt->rtKind), rt->rtName);

  if ((rt->rtRequestPort != argNULL) &&
      (rt->rtRequestPort->argType != itNULL))
    itCheckRequestPortType(rt->rtRequestPort->argName, rt->rtRequestPort->argType);

  if ((rt->rtReplyPort != argNULL) &&
      (rt->rtReplyPort->argType != itNULL))
    itCheckReplyPortType(rt->rtReplyPort->argName, rt->rtReplyPort->argType);

  if ((rt->rtWaitTime != argNULL) &&
      (rt->rtWaitTime->argType != itNULL))
    itCheckIntType(rt->rtWaitTime->argName, rt->rtWaitTime->argType);

  if ((rt->rtMsgOption != argNULL) &&
      (rt->rtMsgOption->argType != itNULL))
    itCheckIntType(rt->rtMsgOption->argName, rt->rtMsgOption->argType);

  if ((IsKernelServer && rt->rtServerImpl) ||
      (IsKernelUser   && rt->rtUserImpl))
    fatal("Implicit data is not supported in the KernelUser and KernelServer modes");
  /* rtCheckTrailerType will hit a fatal() if something goes wrong */
  if (rt->rtServerImpl)
    rtCheckMaskFunction(rt->rtArgs, akbServerImplicit, rtCheckTrailerType);
  if (rt->rtUserImpl)
    rtCheckMaskFunction(rt->rtArgs, akbUserImplicit, rtCheckTrailerType);
}

/*
 * Check for arguments which are missing seemingly needed functions.
 * We make this check here instead of in itCheckDecl, because here
 * we can take into account what kind of argument the type is
 * being used with.
 *
 * These are warnings, not hard errors, because mig will generate
 * reasonable code in any case.  The generated code will work fine
 * if the ServerType and TransType are really the same, even though
 * they have different names.
 */

static void
rtCheckArgTrans(routine_t *rt)
{
  argument_t *arg;

  /* the arg may not have a type (if there was some error in parsing it) */

  for (arg = rt->rtArgs; arg != argNULL; arg = arg->argNext) {
    ipc_type_t *it = arg->argType;

    if ((it != itNULL) && !streql(it->itServerType, it->itTransType)) {
      if (akCheck(arg->argKind, akbSendRcv) && (it->itInTrans == strNULL))
        warn("%s: argument has no in-translation function", arg->argName);

      if (akCheck(arg->argKind, akbReturnSnd) && (it->itOutTrans == strNULL))
        warn("%s: argument has no out-translation function", arg->argName);
    }
  }
}

/*
 * Adds an implicit return-code argument.  It exists in the reply message,
 * where it is the first piece of data (After the NDR format label)..
 */

static void
rtAddRetCode(routine_t *rt)
{
  argument_t *arg = argAlloc();

  arg->argName = "RetCode";
  arg->argType = itRetCodeType;
  arg->argKind = akRetCode;
  rt->rtRetCode = arg;

  arg->argNext = rt->rtArgs;
  rt->rtArgs = arg;
}

/*
 * Process the Return Code.
 * The MIG protocol says that RetCode != 0 are only sent through
 * mig_reply_error_t structures. Therefore, there is no need
 * for reserving a RetCode in a complex Reply message.
 */
static void
rtProcessRetCode(routine_t *rt)
{
  if (!rt->rtOneWay && !rt->rtSimpleReply) {
    argument_t *arg = rt->rtRetCode;

    arg->argKind = akRemFeature(arg->argKind, akbReply);
    /* we want the RetCode to be a local variable instead */
    arg->argKind = akAddFeature(arg->argKind, akbVarNeeded);
  }
  if (rt->rtRetCArg != argNULL && !rt->rtSimpleRequest) {
    argument_t *arg = rt->rtRetCArg;

    arg->argKind = akeRetCode|akbUserArg|akbServerArg|akbSendRcv;
  }
}

/*
 * Adds an implicit NDR argument.  It exists in the reply message,
 * where it is the first piece of data.
 */

static void
rtAddNdrCode(routine_t *rt)
{
  argument_t *arg = argAlloc();

  arg->argName = "NDR_record";
  arg->argType = itNdrCodeType;
  arg->argKind = akeNdrCode;
  rt->rtNdrCode = arg;

  /* add at beginning, so ndr-code is first in the reply message  */
  arg->argNext = rt->rtArgs;
  rt->rtArgs = arg;
}

/*
 * Process the NDR Code.
 * We stick a NDR format label iff there is untyped data
 */
static void
rtProcessNdrCode(routine_t *rt)
{
  argument_t *ndr = rt->rtNdrCode;
  argument_t *arg;
  boolean_t found;

  /* akbSendSnd|akbSendBody initialize the NDR format label */
#define ndr_send akbRequest|akbSend|akbSendSnd|akbSendBody
  /* akbReplyInit initializes the NDR format label */
#define ndr_rcv  akbReply|akbReplyInit|akbReturn|akbReturnBody

  ndr->argKind = akAddFeature(ndr->argKind, ndr_send|ndr_rcv);

  for (found = FALSE, arg = ndr->argNext; arg != argNULL; arg = arg->argNext)
    if (akCheck(arg->argKind, akbSendRcv|akbSendBody) &&
       !akCheck(arg->argKind, akbServerImplicit) && !arg->argType->itPortType &&
       (!arg->argParent || akIdent(arg->argKind) == akeCount ||
       akIdent(arg->argKind) == akeCountInOut)) {
      arg->argKind = akAddFeature(arg->argKind, akbSendNdr);
      found = TRUE;
    }
  if (!found)
    ndr->argKind = akRemFeature(ndr->argKind, ndr_send);

  found = FALSE;
  if (!rt->rtOneWay)
    for (arg = ndr->argNext; arg != argNULL; arg = arg->argNext)
      if ((arg == rt->rtRetCode && akCheck(arg->argKind, akbReply)) ||
          (arg != rt->rtRetCode &&
          akCheck(arg->argKind, akbReturnRcv|akbReturnBody) &&
          !akCheck(arg->argKind, akbUserImplicit) && !arg->argType->itPortType &&
          (!arg->argParent || akIdent(arg->argKind) == akeCount ||
          akIdent(arg->argKind) == akeCountInOut))) {
        arg->argKind = akAddFeature(arg->argKind, akbReturnNdr);
        found = TRUE;
      }
  if (!found && !akCheck(rt->rtRetCode->argKind, akbReply))
    ndr->argKind = akRemFeature(ndr->argKind, ndr_rcv);
}

/*
 *  Adds a dummy WaitTime argument to the function.
 *  This argument doesn't show up in any C argument lists;
 *  it implements the global WaitTime statement.
 */

static void
rtAddWaitTime(routine_t *rt, identifier_t name, arg_kind_t kind)
{
  argument_t *arg = argAlloc();
  argument_t **loc;

  arg->argName = "dummy WaitTime arg";
  arg->argVarName = name;
  arg->argType = itWaitTimeType;
  arg->argKind = kind;
  rt->rtWaitTime = arg;

  /* add wait-time after msg-option, if possible */

  if (rt->rtMsgOption != argNULL)
    loc = &rt->rtMsgOption->argNext;
  else
    loc = &rt->rtArgs;

  arg->argNext = *loc;
  *loc = arg;

  rtSetArgDefaults(rt, arg);
}

/*
 *  Adds a dummy MsgOption argument to the function.
 *  This argument doesn't show up in any C argument lists;
 *  it implements the global MsgOption statement.
 */

static void
rtAddMsgOption(routine_t *rt, identifier_t name)
{
  argument_t *arg = argAlloc();
  argument_t **loc;

  arg->argName = "dummy MsgOption arg";
  arg->argVarName = name;
  arg->argType = itMsgOptionType;
  arg->argKind = akeMsgOption;
  rt->rtMsgOption = arg;

  /* add msg-option after msg-seqno */

  loc = &rt->rtArgs;

  arg->argNext = *loc;
  *loc = arg;

  rtSetArgDefaults(rt, arg);
}

/*
 * Process the MsgOption Code.
 * We must add the information to post a receive with the right
 * Trailer options.
 */
static void
rtProcessMsgOption(routine_t *rt)
{
  argument_t *msgop = rt->rtMsgOption;
  argument_t *arg;
  boolean_t sectoken = FALSE;
  boolean_t audittoken = FALSE;
  boolean_t contexttoken = FALSE;

  for (arg = rt->rtArgs; arg != argNULL; arg = arg->argNext)
    if (akCheckAll(arg->argKind, akbReturn|akbUserImplicit)) {
      if (akIdent(arg->argKind) == akeSecToken)
        sectoken = TRUE;
      else if (akIdent(arg->argKind) == akeAuditToken)
        audittoken = TRUE;
      else if (akIdent(arg->argKind) == akeContextToken)
        contexttoken = TRUE;
    }

  if (contexttoken == TRUE)
    msgop->argVarName = strconcat(msgop->argVarName, "|MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_CTX)");
  else if (audittoken == TRUE)
    msgop->argVarName = strconcat(msgop->argVarName, "|MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AUDIT)");
  else if (sectoken == TRUE)
    msgop->argVarName = strconcat(msgop->argVarName, "|MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_SENDER)");
  /* other implicit data received by the user will be handled here */
}

static void
rtProcessUseSpecialReplyPort(routine_t *rt)
{
  if (IsKernelUser || IsKernelServer) {
    fatal("UseSpecialReplyPort option cannot be used with KernelUser / KernelServer\n");
  }
  rt->rtMsgOption->argVarName = strconcat(rt->rtMsgOption->argVarName, "|__MigSpecialReplyPortMsgOption");
}

/*
 *  Adds a dummy reply port argument to the function.
 */

static void
rtAddDummyReplyPort(routine_t *rt, ipc_type_t *type)
{
  argument_t *arg = argAlloc();
  argument_t **loc;

  arg->argName = "dummy ReplyPort arg";
  arg->argVarName = "dummy ReplyPort arg";
  arg->argType = type;
  arg->argKind = akeReplyPort;
  rt->rtReplyPort = arg;

  /* add the reply port after the request port */

  if (rt->rtRequestPort != argNULL)
    loc = &rt->rtRequestPort->argNext;
  else
    loc = &rt->rtArgs;

  arg->argNext = *loc;
  *loc = arg;

  rtSetArgDefaults(rt, arg);
}


/*
 * At least one overwrite keyword has been detected:
 * we tag all the OOL entries (ports + data) with
 * akbOverwrite which will tell us that we have to
 * fill a KPD entry in the message-template
 */
static void
rtCheckOverwrite(routine_t *rt)
{
  argument_t *arg;
  int howmany = rt->rtOverwrite;

  for (arg = rt->rtArgs; arg != argNULL; arg = arg->argNext) {
    ipc_type_t *it = arg->argType;

    if (akCheck(arg->argKind, akbReturnKPD) && !it->itInLine) {
      /* among OUT args, we want OOL, OOL ports and MigInLine */
      arg->argKind = akAddFeature(arg->argKind, akbOverwrite);
      if (arg->argFlags & flOverwrite)
        howmany--;
      if (!howmany)
        return;
    }
  }
}

/*
 * Initializes argRequestPos, argReplyPos, rtMaxRequestPos, rtMaxReplyPos,
 * rtNumRequestVar, rtNumReplyVar, and adds akbVarNeeded to those arguments
 * that need it because of variable-sized inline considerations.
 *
 * argRequestPos and argReplyPos get -1 if the value shouldn't be used.
 */
static void
rtCheckVariable(routine_t *rt)
{
  argument_t *arg;
  int NumRequestVar = 0;
  int NumReplyVar = 0;
  int MaxRequestPos = 0;
  int MaxReplyPos = 0;

  for (arg = rt->rtArgs; arg != argNULL; arg = arg->argNext) {
    argument_t *parent = arg->argParent;

    /*
     * We skip KPDs. We have to make sure that the KPDs count
     * present in the message body follow the RequestPos/ReplyPos logic
     * The rest of the parameters are defaulted to have
     * Arg{Request, Reply}Pos = 0
     */
    if (parent == argNULL || akCheck(parent->argKind, akbSendKPD|akbReturnKPD)) {
      if (akCheckAll(arg->argKind, akbSend|akbSendBody)) {
        arg->argRequestPos = NumRequestVar;
        MaxRequestPos = NumRequestVar;
        if (akCheck(arg->argKind, akbVariable))
          NumRequestVar++;
      }
      if (akCheckAll(arg->argKind, akbReturn|akbReturnBody)) {
        arg->argReplyPos = NumReplyVar;
        MaxReplyPos = NumReplyVar;
        if (akCheck(arg->argKind, akbVariable))
          NumReplyVar++;
      }
    }
    else {
      arg->argRequestPos = parent->argRequestPos;
      arg->argReplyPos = parent->argReplyPos;
    }
    /*
     printf("Var %s Kind %x RequestPos %d\n", arg->argVarName, arg->argKind, arg->argRequestPos);
     printf("* Var %s Kind %x ReplyPos %d\n", arg->argVarName, arg->argKind, arg->argReplyPos);
     */

    /* Out variables that follow a variable-sized field
       need VarNeeded or ReplyCopy; they can't be stored
       directly into the reply message. */

    if (akCheckAll(arg->argKind, akbReturnSnd|akbReturnBody) &&
        !akCheck(arg->argKind, akbReplyCopy|akbVarNeeded) &&
        (arg->argReplyPos > 0))
      arg->argKind = akAddFeature(arg->argKind, akbVarNeeded);
  }

  rt->rtNumRequestVar = NumRequestVar;
  rt->rtNumReplyVar = NumReplyVar;
  rt->rtMaxRequestPos = MaxRequestPos;
  rt->rtMaxReplyPos = MaxReplyPos;
}

/*
 * Adds akbDestroy where needed.
 */

static void
rtCheckDestroy(routine_t *rt)
{
  argument_t *arg;

  for (arg = rt->rtArgs; arg != argNULL; arg = arg->argNext) {
    ipc_type_t *it = arg->argType;

    if(akCheck(arg->argKind, akbSendRcv) &&
       !akCheck(arg->argKind, akbReturnSnd) &&
       (it->itDestructor != strNULL || IS_MIG_INLINE_EMUL(it))) {
      arg->argKind = akAddFeature(arg->argKind, akbDestroy);
    }
    if (argIsIn(arg) && akCheck(arg->argKind, akbSendKPD|akbReturnKPD) &&
        arg->argKPD_Type == MACH_MSG_OOL_DESCRIPTOR &&
        (arg->argFlags & flAuto))
      arg->argKind = akAddFeature(arg->argKind, akbDestroy);
  }
}

/*
 * Sets ByReferenceUser and ByReferenceServer.
 */

static void
rtAddByReference(routine_t *rt)
{
  argument_t *arg;

  for (arg = rt->rtArgs; arg != argNULL; arg = arg->argNext) {
    ipc_type_t *it = arg->argType;

    if (akCheck(arg->argKind, akbReturnRcv) && it->itStruct) {
      arg->argByReferenceUser = TRUE;

      /*
       *  A CountInOut arg itself is not akbReturnRcv,
       *  so we need to set argByReferenceUser specially.
       */

      if (arg->argCInOut != argNULL)
        arg->argCInOut->argByReferenceUser = TRUE;
    }

    if ((akCheck(arg->argKind, akbReturnSnd) ||
         (akCheck(arg->argKind, akbServerImplicit) &&
          akCheck(arg->argKind, akbReturnRcv) &&
          akCheck(arg->argKind, akbSendRcv)))
        && it->itStruct) {
      arg->argByReferenceServer = TRUE;
    }
  }
}

/*
 * This procedure can be executed only when all the akb* and ake* have
 * been set properly (when rtAddCountArg is executed, akbVarNeeded
 * might not be set yet - see rtCheckVariable)
 */
void
rtAddSameCount(routine_t *rt)
{
  argument_t *arg;

  for (arg = rt->rtArgs; arg != argNULL; arg = arg->argNext)
    if (arg->argFlags & flSameCount) {
      ipc_type_t *it = arg->argType;
      argument_t *tmp_count;
      argument_t *my_count = arg->argCount;
      argument_t *ref_count = arg->argSameCount->argCount;

      tmp_count = argAlloc();
      *tmp_count = *ref_count;
      /*
       * if our count is a akbVarNeeded, we need to copy this
       * attribute to the master count!
       */
      tmp_count->argKind = akeSameCount;
      ref_count->argKind = akAddFeature(ref_count->argKind, akCheck(my_count->argKind, akbVarNeeded));
      tmp_count->argKind = akAddFeature(tmp_count->argKind, akCheck(my_count->argKind, akbVarNeeded));
      tmp_count->argNext = my_count->argNext;
      tmp_count->argMultiplier = my_count->argMultiplier;
      tmp_count->argType = my_count->argType;
      tmp_count->argParent = arg;
      /* we don't need more */
      arg->argCount = tmp_count;
      arg->argNext = tmp_count;
      /* for these args, Cnt is not akbRequest, and therefore size is embedded */
      if (IS_VARIABLE_SIZED_UNTYPED(it))
        it->itMinTypeSize = 0;
      tmp_count->argType->itMinTypeSize = 0;
      tmp_count->argType->itTypeSize = 0;
    }
}

void
rtCheckRoutine(routine_t *rt)
{
  /* Initialize random fields. */

  rt->rtErrorName = ErrorProc;
  rt->rtOneWay = (rt->rtKind == rkSimpleRoutine);
  rt->rtServerName = strconcat(ServerPrefix, rt->rtName);
  rt->rtUserName = strconcat(UserPrefix, rt->rtName);
  rt->rtUseSpecialReplyPort = UseSpecialReplyPort && !rt->rtOneWay;
  rt->rtConsumeOnSendError = ConsumeOnSendError;

  /* Add implicit arguments. */

  rtAddRetCode(rt);
  rtAddNdrCode(rt);

  /* Check out the arguments and their types.  Add count, poly
   implicit args.  Any arguments added after rtCheckRoutineArgs
   should have rtSetArgDefaults called on them. */

  rtCheckRoutineArgs(rt);

  /* Add dummy WaitTime and MsgOption arguments, if the routine
   doesn't have its own args and the user specified global values. */

  if (rt->rtReplyPort == argNULL) {
    if (rt->rtOneWay)
      rtAddDummyReplyPort(rt, itZeroReplyPortType);
    else
      rtAddDummyReplyPort(rt, itRealReplyPortType);
  } else if (akCheck(rt->rtReplyPort->argKind, akbUserArg)) {
      /* If an explicit ReplyPort is used, we can't assume it will be the SRP */
      rt->rtUseSpecialReplyPort = FALSE;
  }
  if (rt->rtMsgOption == argNULL) {
    if (MsgOption == strNULL)
      rtAddMsgOption(rt, "MACH_MSG_OPTION_NONE");
    else
      rtAddMsgOption(rt, MsgOption);
  }
  if (rt->rtWaitTime == argNULL) {
    if (WaitTime != strNULL)
      rtAddWaitTime(rt, WaitTime, akeWaitTime);
    else if (SendTime != strNULL)
      rtAddWaitTime(rt, SendTime, akeSendTime);
  }
  if (rt->rtWaitTime && rt->rtConsumeOnSendError == ConsumeOnSendErrorNone) {
    warn("%s %s specifies a SendTime/WaitTime which may leak resources, "
      "adopt \"ConsumeOnSendError Timeout\"",
      rtRoutineKindToStr(rt->rtKind), rt->rtName);
  }


  /* Now that all the arguments are in place, do more checking. */

  rtCheckArgTypes(rt);
  rtCheckArgTrans(rt);

  if (rt->rtOneWay) {
    if (rtCheckMask(rt->rtArgs, akbReturn) || rt->rtUserImpl)
      error("%s %s has OUT argument", rtRoutineKindToStr(rt->rtKind), rt->rtName);
  }

  /* If there were any errors, don't bother calculating more info
   that is only used in code generation anyway.  Therefore,
   the following functions don't have to worry about null types. */

  if (mig_errors > 0)
    fatal("%d errors found. Abort.\n", mig_errors);

  rt->rtServerImpl  = rtCountMask(rt->rtArgs, akbServerImplicit);
  rt->rtUserImpl = rtCountMask(rt->rtArgs, akbUserImplicit);
  /*
   * ASSUMPTION:
   * kernel cannot change a message from simple to complex,
   * therefore SimpleSendReply and SimpleRcvReply become SimpleReply
   */
  rtCheckSimple(rt->rtArgs, akbRequest, &rt->rtSimpleRequest);
  rtCheckSimple(rt->rtArgs, akbReply, &rt->rtSimpleReply);

  rt->rtRequestKPDs = rtCountKPDs(rt->rtArgs, akbSendKPD);
  rt->rtReplyKPDs = rtCountKPDs(rt->rtArgs, akbReturnKPD);
  /*
   * Determine how many overwrite parameters we have:
   *  # of Overwrite args -> rt->rtOverwrite
   *  flOverwrite -> the arg has to be overwritten
   *  akbOverwrite -> the arg has to be declared in the message-template
   *  (only as a placeholder if !flOverwrite).
   */
  if ((rt->rtOverwrite = rtCountFlags(rt->rtArgs, flOverwrite))) {
    rtCheckOverwrite(rt);
    rt->rtOverwriteKPDs = rtCountKPDs(rt->rtArgs, akbReturnKPD|akbOverwrite);
    if (IsKernelUser)
      fatal("Overwrite option(s) do not match with the KernelUser personality\n");
  }

  rtCheckFit(rt, akbRequest, &rt->rtRequestFits, &rt->rtRequestUsedLimit, &rt->rtRequestSizeKnown);
  rtCheckFit(rt, akbReply, &rt->rtReplyFits, &rt->rtReplyUsedLimit, &rt->rtReplySizeKnown);

  rtCheckVariable(rt);
  rtCheckDestroy(rt);
  rtAddByReference(rt);
  rtSuffixExtArg(rt->rtArgs);
  rtAddSameCount(rt);
  rtProcessRetCode(rt);
  rtProcessNdrCode(rt);
  if (rt->rtUserImpl)
    rtProcessMsgOption(rt);
  if (rt->rtUseSpecialReplyPort)
    rtProcessUseSpecialReplyPort(rt);

  rt->rtNoReplyArgs = !rtCheckMask(rt->rtArgs, akbReturnSnd);

  if (UseEventLogger)
  /* some more info's are needed for Event logging/Stats */
    rtFindHowMany(rt);
}

void
rtMinRequestSize(FILE *file, routine_t *rt, char *str)
{
  fprintf(file, "(mach_msg_size_t)(sizeof(%s)", str);
  rtSizeDelta(file, akbRequest, rt);
  fprintf(file, ")");
}

void
rtMinReplySize(FILE *file, routine_t *rt, char *str)
{
  fprintf(file, "(mach_msg_size_t)(sizeof(%s)", str);
  rtSizeDelta(file, akbReply, rt);
  fprintf(file, ")");
}

static void
rtSizeDelta(FILE *file, u_int mask, routine_t *rt)
{
  argument_t *arg;
  u_int min_size = sizeof(mach_msg_header_t);
  u_int max_size;
  boolean_t output = FALSE;

  if (!rt->rtSimpleRequest)
    machine_alignment(min_size, sizeof(mach_msg_body_t));
  max_size = min_size;
  for (arg = rt->rtArgs; arg != argNULL; arg = arg->argNext)
    if (akCheck(arg->argKind, mask)) {
      ipc_type_t *it = arg->argType;

      machine_alignment(min_size, it->itMinTypeSize);
      machine_alignment(max_size, it->itMinTypeSize);

      if (IS_VARIABLE_SIZED_UNTYPED(it)) {
        machine_alignment(max_size, it->itTypeSize);
        max_size += it->itPadSize;
      }
      if (IS_OPTIONAL_NATIVE(it)) {
        if (output)
          fprintf(file, " + ");
        else {
          output = TRUE;
          fprintf(file, " - (");
        }
        fprintf(file, "_WALIGNSZ_(%s)", it->itUserType);
      }
    }
  if (min_size != max_size) {
    if (output)
      fprintf(file, " + ");
    else
      fprintf(file, " - ");
    fprintf(file, "%d", max_size - min_size);
  }
  if (output)
    fprintf(file, ")");
}