summaryrefslogtreecommitdiff
path: root/src/icont/lcode.c
blob: a1481f12c7e63bf1b968905cf2605366ef7ececc (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
/*
 * lcode.c -- linker routines to parse .u1 files and produce icode.
 */

#include "link.h"
#include "tproto.h"
#include "tglobals.h"
#include "opcode.h"
#include "keyword.h"
#include "../h/version.h"
#include "../h/header.h"

/*
 *  This needs fixing ...
 */
#undef CsetPtr
#define CsetPtr(b,c)	((c) + (((b)&0377) >> LogIntBits))

/*
 * Prototypes.
 */

static void	align		(void);
static void	backpatch	(int lab);
static void	clearlab	(void);
static void	flushcode	(void);
static void	intout		(int oint);
static void	lemit		(int op,char *name);
static void	lemitcon	(int k);
static void	lemitin		(int op,word offset,int n,char *name);
static void	lemitint	(int op,long i,char *name);
static void	lemitl		(int op,int lab,char *name);
static void	lemitn		(int op,word n,char *name);
static void	lemitproc    (word name,int nargs,int ndyn,int nstat,int fstat);
static void	lemitr		(int op,word loc,char *name);
static void	misalign	(void);
static void	outblock	(char *addr,int count);
static void	setfile		(void);
static void	wordout		(word oword);

#ifdef FieldTableCompression
   static void	charout		(unsigned char oint);
   static void	shortout	(short oint);
#endif					/* FieldTableCompression */

#ifdef DeBugLinker
   static void	dumpblock	(char *addr,int count);
#endif					/* DeBugLinker */

word pc = 0;		/* simulated program counter */

#define outword(n)	wordout((word)(n))
#define outop(n)	intout((int)(n))
#define outchar(n)	charout((unsigned char)(n))
#define outshort(n)	shortout((short)(n))
#define CodeCheck(n) if ((long)codep + (n) > (long)((long)codeb + maxcode))\
                     codeb = (char *) trealloc(codeb, &codep, &maxcode, 1,\
                       (n), "code buffer");

#define ByteBits 8

/*
 * gencode - read .u1 file, resolve variable references, and generate icode.
 *  Basic process is to read each line in the file and take some action
 *  as dictated by the opcode.	This action sometimes involves parsing
 *  of arguments and usually culminates in the call of the appropriate
 *  lemit* routine.
 */
void gencode()
   {
   register int op, k, lab;
   int j, nargs, flags, implicit;
   char *name;
   word id, procname;
   struct centry *cp;
   struct gentry *gp;
   struct fentry *fp;
   union xval gg;

   while ((op = getopc(&name)) != EOF) {
      switch (op) {

         /* Ternary operators. */

         case Op_Toby:
         case Op_Sect:

         /* Binary operators. */

         case Op_Asgn:
         case Op_Cat:
         case Op_Diff:
         case Op_Div:
         case Op_Eqv:
         case Op_Inter:
         case Op_Lconcat:
         case Op_Lexeq:
         case Op_Lexge:
         case Op_Lexgt:
         case Op_Lexle:
         case Op_Lexlt:
         case Op_Lexne:
         case Op_Minus:
         case Op_Mod:
         case Op_Mult:
         case Op_Neqv:
         case Op_Numeq:
         case Op_Numge:
         case Op_Numgt:
         case Op_Numle:
         case Op_Numlt:
         case Op_Numne:
         case Op_Plus:
         case Op_Power:
         case Op_Rasgn:
         case Op_Rswap:
         case Op_Subsc:
         case Op_Swap:
         case Op_Unions:

         /* Unary operators. */

         case Op_Bang:
         case Op_Compl:
         case Op_Neg:
         case Op_Nonnull:
         case Op_Null:
         case Op_Number:
         case Op_Random:
         case Op_Refresh:
         case Op_Size:
         case Op_Tabmat:
         case Op_Value:

         /* Instructions. */

         case Op_Bscan:
         case Op_Ccase:
         case Op_Coact:
         case Op_Cofail:
         case Op_Coret:
         case Op_Dup:
         case Op_Efail:
         case Op_Eret:
         case Op_Escan:
         case Op_Esusp:
         case Op_Limit:
         case Op_Lsusp:
         case Op_Pfail:
         case Op_Pnull:
         case Op_Pop:
         case Op_Pret:
         case Op_Psusp:
         case Op_Push1:
         case Op_Pushn1:
         case Op_Sdup:
            newline();
            lemit(op, name);
            break;

         case Op_Chfail:
         case Op_Create:
         case Op_Goto:
         case Op_Init:
            lab = getlab();
            newline();
            lemitl(op, lab, name);
            break;

         case Op_Cset:
         case Op_Real:
            k = getdec();
            newline();
            lemitr(op, lctable[k].c_pc, name);
            break;

         case Op_Field:
            id = getid();
            newline();
            fp = flocate(id);
            if (fp != NULL)
               lemitn(op, (word)(fp->f_fid-1), name);
	    else
               lemitn(op, (word)-1, name);	/* no warning any more */
            break;


         case Op_Int: {
            long i;
            k = getdec();
            newline();
            cp = &lctable[k];
            /*
             * Check to see if a large integers has been converted to a string.
             *  If so, generate the code for +s.
             */
            if (cp->c_flag & F_StrLit) {
               lemit(Op_Pnull,"pnull");
               lemitin(Op_Str, cp->c_val.sval, cp->c_length, "str");
               lemit(Op_Number,"number");
               break;
               }
            i = (long)cp->c_val.ival;
            lemitint(op, i, name);
            break;
            }


         case Op_Invoke:
            k = getdec();
            newline();
            if (k == -1)
               lemit(Op_Apply,"apply");
            else
               lemitn(op, (word)k, name);
            break;

         case Op_Keywd:
            id = getstr();
            newline();
            k = klookup(&lsspace[id]);
            switch (k) {
               case 0:
                  lfatal(&lsspace[id],"invalid keyword");
                  break;
               case K_FAIL:
                  lemit(Op_Efail,"efail");
                  break;
               case K_NULL:
                  lemit(Op_Pnull,"pnull");
                  break;
               default:
               lemitn(op, (word)k, name);
            }
            break;

         case Op_Llist:
            k = getdec();
            newline();
            lemitn(op, (word)k, name);
            break;

         case Op_Lab:
            lab = getlab();
            newline();
            #ifdef DeBugLinker
               if (Dflag)
                  fprintf(dbgfile, "L%d:\n", lab);
            #endif			/* DeBugLinker */
            backpatch(lab);
            break;

         case Op_Line:
            /*
             * Line number change.
             *  All the interesting stuff happens in Op_Colm now.
             */
            lineno = getdec();

            #ifndef SrcColumnInfo
               /*
                * Enter the value in the line number table
                *  that is stored in the icode file and used during error
                *  handling and execution monitoring.  One can generate a VM
                *  instruction for these changes, but since the numbers are not
                *  saved and restored during backtracking, it is more accurate
                *  to check for line number changes in-line in the interpreter.
                *  Fortunately, the in-line check is about as fast as executing
                *  Op_Line instructions.  All of this is complicated by the use
                *  of Op_Line to generate Noop instructions when enabled by the
                *  LineCodes #define.
                *
                * If SrcColumnInfo is required, this code is duplicated,
                *  with changes, in the Op_Colm case below.
                */
               if (lnfree >= &lntable[nsize])
                  lntable  = (struct ipc_line *)trealloc(lntable,&lnfree,&nsize,
                     sizeof(struct ipc_line), 1, "line number table");
               lnfree->ipc = pc;
               lnfree->line = lineno;
               lnfree++;
            #endif			/* SrcColumnInfo */

            /*
             * Could generate an Op_Line for monitoring, but don't anymore:
             *
             * lemitn(op, (word)lineno, name);
             */

            newline();

            #ifdef LineCodes
               #ifndef EventMon
                  lemit(Op_Noop,"noop");
               #endif			/* EventMon */
            #endif			/* LineCodes */

            break;

         case Op_Colm:			/* always recognize, maybe ignore */

            colmno = getdec();
            #ifdef SrcColumnInfo
               if (lnfree >= &lntable[nsize])
                  lntable  = (struct ipc_line *)trealloc(lntable,&lnfree,&nsize,
                     sizeof(struct ipc_line), 1, "line number table");
               lnfree->ipc = pc;
               lnfree->line = lineno + (colmno << 16);
               lnfree++;
            #endif			/* SrcColumnInfo */
            break;

         case Op_Mark:
            lab = getlab();
            newline();
            lemitl(op, lab, name);
            break;

         case Op_Mark0:
            lemit(op, name);
            break;

         case Op_Str:
            k = getdec();
            newline();
            cp = &lctable[k];
            lemitin(op, cp->c_val.sval, cp->c_length, name);
            break;

         case Op_Tally:
            k = getdec();
            newline();
            lemitn(op, (word)k, name);
            break;

         case Op_Unmark:
            lemit(Op_Unmark, name);
            break;

         case Op_Var:
            k = getdec();
            newline();
            flags = lltable[k].l_flag;
            if (flags & F_Global)
               lemitn(Op_Global, (word)(lltable[k].l_val.global->g_index),
                  "global");
            else if (flags & F_Static)
               lemitn(Op_Static, (word)(lltable[k].l_val.staticid-1), "static");
            else if (flags & F_Argument)
               lemitn(Op_Arg, (word)(lltable[k].l_val.offset-1), "arg");
            else
               lemitn(Op_Local, (word)(lltable[k].l_val.offset-1), "local");
            break;

         /* Declarations. */

         case Op_Proc:
            getstr();
            newline();
            procname = putident(strlen(&lsspace[lsfree]) + 1, 0);
            if (procname >= 0 && (gp = glocate(procname)) != NULL) {
               /*
                * Initialize for wanted procedure.
                */
               locinit();
               clearlab();
               lineno = 0;
               implicit = gp->g_flag & F_ImpError;
               nargs = gp->g_nargs;
	       align();
               #ifdef DeBugLinker
                  if (Dflag)
                     fprintf(dbgfile, "\n# procedure %s\n", &lsspace[lsfree]);
               #endif			/* DeBugLinker */
               }
            else {
               /*
                * Skip unreferenced procedure.
                */
               while ((op = getopc(&name)) != EOF && op != Op_End)
                  if (op == Op_Filen)
                     setfile();		/* handle filename op while skipping */
                  else
                     newline();		/* ignore everything else */
               }
            break;

         case Op_Local:
            k = getdec();
            flags = getoct();
            id = getid();
            putlocal(k, id, flags, implicit, procname);
            break;

         case Op_Con:
            k = getdec();
            flags = getoct();
            if (flags & F_IntLit) {
               {
               long m;
               word s_indx;

               j = getdec();		/* number of characters in integer */
               m = getint(j,&s_indx);	/* convert if possible */
               if (m < 0) {		/* negative indicates integer too big */
                  gg.sval = s_indx;	/* convert to a string */
                  putconst(k, F_StrLit, j, pc, &gg);
                  }
               else {			/* integers is small enough */
                  gg.ival = m;
                  putconst(k, flags, 0, pc, &gg);
                  }
               }
               }
            else if (flags & F_RealLit) {
               gg.rval = getreal();
               putconst(k, flags, 0, pc, &gg);
               }
            else if (flags & F_StrLit) {
               j = getdec();
               gg.sval = getstrlit(j);
               putconst(k, flags, j, pc, &gg);
               }
            else if (flags & F_CsetLit) {
               j = getdec();
               gg.sval = getstrlit(j);
               putconst(k, flags, j, pc, &gg);
               }
            else
               fprintf(stderr, "gencode: illegal constant\n");
            newline();
            lemitcon(k);
            break;

         case Op_Filen:
            setfile();
            break;

         case Op_Declend:
            newline();
            gp->g_pc = pc;
            lemitproc(procname, nargs, dynoff, lstatics-static1, static1);
            break;

         case Op_End:
            newline();
            flushcode();
            break;

         default:
            fprintf(stderr, "gencode: illegal opcode(%d): %s\n", op, name);
            newline();
         }
      }
   }

/*
 * setfile - handle Op_Filen.
 */
static void setfile()
   {
   if (fnmfree >= &fnmtbl[fnmsize])
      fnmtbl = (struct ipc_fname *) trealloc(fnmtbl, &fnmfree,
         &fnmsize, sizeof(struct ipc_fname), 1, "file name table");
   fnmfree->ipc = pc;
   fnmfree->fname = getrest();
   strcpy(icnname, &lsspace[fnmfree->fname]);
   fnmfree++;
   newline();
   }

/*
 *  lemit - emit opcode.
 *  lemitl - emit opcode with reference to program label.
 *	for a description of the chaining and backpatching for labels.
 *  lemitn - emit opcode with integer argument.
 *  lemitr - emit opcode with pc-relative reference.
 *  lemitin - emit opcode with reference to identifier table & integer argument.
 *  lemitint - emit word opcode with integer argument.
 *  lemitcon - emit constant table entry.
 *  lemitproc - emit procedure block.
 *
 * The lemit* routines call out* routines to effect the "outputting" of icode.
 *  Note that the majority of the code for the lemit* routines is for debugging
 *  purposes.
 */
static void lemit(op, name)
int op;
char *name;
   {

   #ifdef DeBugLinker
      if (Dflag)
         fprintf(dbgfile, "%ld:\t%d\t\t\t\t# %s\n", (long)pc, op, name);
   #endif				/* DeBugLinker */

   outop(op);
   }

static void lemitl(op, lab, name)
int op, lab;
char *name;
   {
   misalign();

   #ifdef DeBugLinker
      if (Dflag)
         fprintf(dbgfile, "%ld:\t%d\tL%d\t\t\t# %s\n", (long)pc, op, lab, name);
   #endif				/* DeBugLinker */

   if (lab >= maxlabels)
      labels  = (word *) trealloc(labels, NULL, &maxlabels, sizeof(word),
         lab - maxlabels + 1, "labels");
   outop(op);
   if (labels[lab] <= 0) {		/* forward reference */
      outword(labels[lab]);
      labels[lab] = WordSize - pc;	/* add to front of reference chain */
      }
   else					/* output relative offset */
      outword(labels[lab] - (pc + WordSize));
   }

static void lemitn(op, n, name)
int op;
word n;
char *name;
   {
   misalign();

   #ifdef DeBugLinker
      if (Dflag)
         fprintf(dbgfile, "%ld:\t%d\t%ld\t\t\t# %s\n", (long)pc, op, (long)n,
            name);
   #endif				/* DeBugLinker */

   outop(op);
   outword(n);
   }


static void lemitr(op, loc, name)
int op;
word loc;
char *name;
   {
   misalign();

   loc -= pc + ((IntBits/ByteBits) + WordSize);

   #ifdef DeBugLinker
      if (Dflag) {
         if (loc >= 0)
            fprintf(dbgfile, "%ld:\t%d\t*+%ld\t\t\t# %s\n",(long) pc, op,
               (long)loc, name);
         else
            fprintf(dbgfile, "%ld:\t%d\t*-%ld\t\t\t# %s\n",(long) pc, op,
               (long)-loc, name);
         }
   #endif				/* DeBugLinker */

   outop(op);
   outword(loc);
   }

static void lemitin(op, offset, n, name)
int op, n;
word offset;
char *name;
   {
   misalign();

   #ifdef DeBugLinker
      if (Dflag)
         fprintf(dbgfile, "%ld:\t%d\t%d,S+%ld\t\t\t# %s\n", (long)pc, op, n,
            (long)offset, name);
   #endif				/* DeBugLinker */

   outop(op);
   outword(n);
   outword(offset);
   }

/*
 * lemitint can have some pitfalls.  outword is used to output the
 *  integer and this is picked up in the interpreter as the second
 *  word of a short integer.  The integer value output must be
 *  the same size as what the interpreter expects.  See op_int and op_intx
 *  in interp.s
 */
static void lemitint(op, i, name)
int op;
long i;
char *name;
   {
   misalign();

   #ifdef DeBugLinker
      if (Dflag)
         fprintf(dbgfile,"%ld:\t%d\t%ld\t\t\t# %s\n",(long)pc,op,(long)i,name);
   #endif				/* DeBugLinker */

   outop(op);
   outword(i);
   }

static void lemitcon(k)
register int k;
   {
   register int i, j;
   register char *s;
   int csbuf[CsetSize];
   union {
      char ovly[1];  /* Array used to overlay l and f on a bytewise basis. */
      long l;
      double f;
      } x;

   if (lctable[k].c_flag & F_RealLit) {

      #ifdef Double
         /* access real values one word at a time */
         int *rp, *rq;
         rp = (int *) &(x.f);
         rq = (int *) &(lctable[k].c_val.rval);
         *rp++ = *rq++;
         *rp = *rq;
      #else				/* Double */
         x.f = lctable[k].c_val.rval;
      #endif				/* Double */

      #ifdef DeBugLinker
         if (Dflag) {
            fprintf(dbgfile,"%ld:\t%d\t\t\t\t# real(%g)",(long)pc,T_Real, x.f);
            dumpblock(x.ovly,sizeof(double));
            }
      #endif				/* DeBugLinker */

      outword(T_Real);

      #ifdef Double
         #if WordBits != 64
            /* fill out real block with an empty word */
            outword(0);
            #ifdef DeBugLinker
               if (Dflag)
	          fprintf(dbgfile,"\t0\t\t\t\t\t# padding\n");
            #endif			/* DeBugLinker */
         #endif				/* WordBits != 64 */
      #endif				/* Double */

      outblock(x.ovly,sizeof(double));
      }
   else if (lctable[k].c_flag & F_CsetLit) {
      for (i = 0; i < CsetSize; i++)
         csbuf[i] = 0;
      s = &lsspace[lctable[k].c_val.sval];
      i = lctable[k].c_length;
      while (i--) {
         Setb(*s, csbuf);
         s++;
         }
      j = 0;
      for (i = 0; i < 256; i++) {
         if (Testb(i, csbuf))
           j++;
         }

      #ifdef DeBugLinker
         if (Dflag) {
            fprintf(dbgfile, "%ld:\t%d\n",(long) pc, T_Cset);
            fprintf(dbgfile, "\t%d\n",j);
            }
      #endif				/* DeBugLinker */

      outword(T_Cset);
      outword(j);		   /* cset size */
      outblock((char *)csbuf,sizeof(csbuf));

      #ifdef DeBugLinker
         if (Dflag)
            dumpblock((char *)csbuf,CsetSize);
      #endif				/* DeBugLinker */

      }
   }

static void lemitproc(name, nargs, ndyn, nstat, fstat)
word name;
int nargs, ndyn, nstat, fstat;
   {
   register int i;
   register char *p;
   word s_indx;
   int size;
   /*
    * FncBlockSize = sizeof(BasicFncBlock) +
    *  sizeof(descrip)*(# of args + # of dynamics + # of statics).
    */
   size = (9*WordSize) + (2*WordSize) * (abs(nargs)+ndyn+nstat);

   p = &lsspace[name];
   #ifdef DeBugLinker
      if (Dflag) {
         fprintf(dbgfile, "%ld:\t%d\n", (long)pc, T_Proc); /* type code */
         fprintf(dbgfile, "\t%d\n", size);		/* size of block */
         fprintf(dbgfile, "\tZ+%ld\n",(long)(pc+size));	/* entry point */
         fprintf(dbgfile, "\t%d\n", nargs);		/* # arguments */
         fprintf(dbgfile, "\t%d\n", ndyn);		/* # dynamic locals */
         fprintf(dbgfile, "\t%d\n", nstat);		/* # static locals */
         fprintf(dbgfile, "\t%d\n", fstat);		/* first static */
         fprintf(dbgfile, "\t%d\tS+%ld\t\t\t# %s\n",	/* name of procedure */
            (int)strlen(p), (long)(name), p);
      }
   #endif				/* DeBugLinker */

   outword(T_Proc);
   outword(size);
   outword(pc + size - 2*WordSize); /* Have to allow for the two words
					that we've already output. */
   outword(nargs);
   outword(ndyn);
   outword(nstat);
   outword(fstat);
   outword(strlen(p));          /* procedure name: length & offset */
   outword(name);

   /*
    * Output string descriptors for argument names by looping through
    *  all locals, and picking out those with F_Argument set.
    */
   for (i = 0; i <= nlocal; i++) {
      if (lltable[i].l_flag & F_Argument) {
         s_indx = lltable[i].l_name;
         p = &lsspace[s_indx];

         #ifdef DeBugLinker
            if (Dflag)
               fprintf(dbgfile, "\t%d\tS+%ld\t\t\t# %s\n", (int)strlen(p),
                  (long)s_indx, p);
         #endif				/* DeBugLinker */

         outword(strlen(p));
         outword(s_indx);
         }
      }

   /*
    * Output string descriptors for local variable names.
    */
   for (i = 0; i <= nlocal; i++) {
      if (lltable[i].l_flag & F_Dynamic) {
         s_indx = lltable[i].l_name;
         p = &lsspace[s_indx];

         #ifdef DeBugLinker
            if (Dflag)
               fprintf(dbgfile, "\t%d\tS+%ld\t\t\t# %s\n", (int)strlen(p),
                  (long)s_indx, p);
         #endif				/* DeBugLinker */

         outword(strlen(p));
         outword(s_indx);
         }
      }

   /*
    * Output string descriptors for static variable names.
    */
   for (i = 0; i <= nlocal; i++) {
      if (lltable[i].l_flag & F_Static) {
         s_indx = lltable[i].l_name;
         p = &lsspace[s_indx];

         #ifdef DeBugLinker
            if (Dflag)
               fprintf(dbgfile, "\t%d\tS+%ld\t\t\t# %s\n", (int)strlen(p),
                  (long)s_indx, p);
         #endif				/* DeBugLinker */

         outword(strlen(p));
         outword(s_indx);
         }
      }
   }

/*
 * gentables - generate interpreter code for global, static,
 *  identifier, and record tables, and built-in procedure blocks.
 */

void gentables()
   {
   register int i;
   register char *s;
   register struct gentry *gp;
   struct fentry *fp;
   struct rentry *rp;
   struct header hdr;

   /*
    * Output record constructor procedure blocks.
    */
   align();
   hdr.Records = pc;

   #ifdef DeBugLinker
      if (Dflag) {
         fprintf(dbgfile, "\n\n# global tables\n");
         fprintf(dbgfile, "\n%ld:\t%d\t\t\t\t# record blocks\n",
	    (long)pc, nrecords);
         }
   #endif				/* DeBugLinker */

   outword(nrecords);
   for (gp = lgfirst; gp != NULL; gp = gp->g_next) {
      if ((gp->g_flag & F_Record) && gp->g_procid > 0) {
         s = &lsspace[gp->g_name];
         gp->g_pc = pc;

         #ifdef DeBugLinker
            if (Dflag) {
               fprintf(dbgfile, "%ld:\n", pc);
               fprintf(dbgfile, "\t%d\n", T_Proc);
               fprintf(dbgfile, "\t%d\n", RkBlkSize(gp));
               fprintf(dbgfile, "\t_mkrec\n");
               fprintf(dbgfile, "\t%d\n", gp->g_nargs);
               fprintf(dbgfile, "\t-2\n");
               fprintf(dbgfile, "\t%d\n", gp->g_procid);
               fprintf(dbgfile, "\t1\n");
               fprintf(dbgfile, "\t%d\tS+%ld\t\t\t# %s\n", (int)strlen(s),
                  (long)gp->g_name, s);
               }
         #endif				/* DeBugLinker */

         outword(T_Proc);		/* type code */
         outword(RkBlkSize(gp));
         outword(0);			/* entry point (filled in by interp)*/
         outword(gp->g_nargs);		/* number of fields */
         outword(-2);			/* record constructor indicator */
         outword(gp->g_procid);		/* record id */
         outword(1);			/* serial number */
         outword(strlen(s));		/* name of record: size and offset */
         outword(gp->g_name);

         for (i=0;i<gp->g_nargs;i++) {	/* field names (filled in by interp) */
            int foundit = 0;
            /*
             * Find the field list entry corresponding to field i in
             * record gp, then write out a descriptor for it.
             */
            for (fp = lffirst; fp != NULL; fp = fp->f_nextentry) {
               for (rp = fp->f_rlist; rp!= NULL; rp=rp->r_link) {
                  if (rp->r_gp == gp && rp->r_fnum == i) {
                     if (foundit) {
                        /*
                         * This internal error should never occur
                         */
                        fprintf(stderr,"found rec %d field %d already!!\n",
                           gp->g_procid, i);
                        fflush(stderr);
                        exit(1);
                        }
                     #ifdef DeBugLinker
                        if (Dflag)
                           fprintf(dbgfile, "\t%d\tS+%ld\t\t\t# %s\n",
                              (int)strlen(&lsspace[fp->f_name]),
                              fp->f_name, &lsspace[fp->f_name]);
                     #endif		/* DeBugLinker */
                     outword(strlen(&lsspace[fp->f_name]));
                     outword(fp->f_name);
                     foundit++;
                     }
                  }
               }
            if (!foundit) {
               /*
                * This internal error should never occur
                */
               fprintf(stderr,"never found rec %d field %d!!\n",
                       gp->g_procid,i);
               fflush(stderr);
               exit(1);
               }
            }
         }
      }

   #ifndef FieldTableCompression

      /*
       * Output record/field table (not compressed).
       */
      hdr.Ftab = pc;
      #ifdef DeBugLinker
         if (Dflag)
            fprintf(dbgfile,"\n%ld:\t\t\t\t\t# record/field table\n",(long)pc);
      #endif				/* DeBugLinker */

      for (fp = lffirst; fp != NULL; fp = fp->f_nextentry) {
         #ifdef DeBugLinker
            if (Dflag)
               fprintf(dbgfile, "%ld:\t\t\t\t\t# %s\n", (long)pc,
                  &lsspace[fp->f_name]);
         #endif				/* DeBugLinker */
         rp = fp->f_rlist;
         for (i = 1; i <= nrecords; i++) {
            while (rp != NULL && rp->r_gp->g_procid < 0)
	       rp = rp->r_link;		/* skip unreferenced constructor */
            if (rp != NULL && rp->r_gp->g_procid == i) {
               #ifdef DeBugLinker
                  if (Dflag)
                      fprintf(dbgfile, "\t%d\n", rp->r_fnum);
               #endif			/* DeBugLinker */
               outop(rp->r_fnum);
               rp = rp->r_link;
	       }
            else {
               #ifdef DeBugLinker
                  if (Dflag)
                      fprintf(dbgfile, "\t-1\n");
               #endif			/* DeBugLinker */
               outop(-1);
               }
            #ifdef DeBugLinker
               if (Dflag && (i == nrecords || (i & 03) == 0))
                  putc('\n', dbgfile);
            #endif			/* DeBugLinker */
            }
         }

   #else				/* FieldTableCompression */

      /*
       * Output record/field table (compressed).
       * This code has not been tested recently.
       */
      {
      int counter = 0, f_num, first, begin, end, entries;
      int *f_fo, *f_row, *f_tabp;
      char *f_bm;
      int pointer, first_avail = 0, inserted, bytes;
      hdr.Fo = pc;

      /*
       * Compute the field width required for this binary;
       * it is determined by the maximum # of fields in any one record.
       */
      long ct = 0;
      for (gp = lgfirst; gp != NULL; gp = gp->g_next)
         if ((gp->g_flag & F_Record) && gp->g_procid > 0)
            if (gp->g_nargs > ct) ct=gp->g_nargs;
      if (ct > 65535L) hdr.FtabWidth = 4;
      else if (ct > 254) hdr.FtabWidth = 2; /* 255 is (not present) */
      else hdr.FtabWidth = 1;

      /* Find out how many field names there are. */
      hdr.Nfields = 0;
      for (fp = lffirst; fp != NULL; fp = fp->f_nextentry)
         hdr.Nfields++;

      entries = hdr.Nfields * nrecords / 4 + 1;
      f_tabp = malloc (entries * sizeof (int));
      for (i = 0; i < entries; i++)
         f_tabp[i] = -1;
      f_fo = malloc (hdr.Nfields * sizeof (int));
   
      bytes = nrecords / 8;
      if (nrecords % 8 != 0)
         bytes++;
      f_bm = calloc (hdr.Nfields, bytes);
      f_row = malloc (nrecords * sizeof (int));
      f_num = 0;

      for (fp = lffirst; fp != NULL; fp = fp->f_nextentry) {
         rp = fp->f_rlist;
         first = 1;
         for (i = 0; i < nrecords; i++) {
            while (rp != NULL && rp->r_gp->g_procid < 0)
   	    rp = rp->r_link;		/* skip unreferenced constructor */
            if (rp != NULL && rp->r_gp->g_procid == i + 1) {
               if (first) {
                  first = 0;
                  begin = end = i;
                  }
               else
                  end = i;
               f_row[i] = rp->r_fnum;
               rp = rp->r_link;
   	       }
            else {
               f_row[i] = -1;
               }
            }

         inserted = 0;
         pointer = first_avail;
         while (!inserted) {
            inserted = 1;
            for (i = begin; i <= end; i++) {
               if (pointer + (end - begin) >= entries) {
                  int j;
                  int old_entries = entries;
                  entries *= 2;
                  f_tabp = realloc (f_tabp, entries * sizeof (int));
                  for (j = old_entries; j < entries; j++)
                     f_tabp[j] = -1;
                  }
               if (f_row[i] != -1)
                  if (f_tabp[pointer + (i - begin)] != -1) {
                     inserted = 0;
                     break;
                     }
               }
            pointer++;
            }
         pointer--;

         /* Create bitmap */
         for (i = 0; i < nrecords; i++) {
            int index = f_num * bytes + i / 8;
   				/* Picks out byte within bitmap row */
            if (f_row[i] != -1) {
               f_bm[index] |= 01;
   	       }
            if (i % 8 != 7)
               f_bm [index] <<= 1;
   	    }

         if (nrecords%8)
            f_bm[(f_num + 1) * bytes - 1] <<= 7 - (nrecords % 8);

         f_fo[f_num++] = pointer - begin;
         /* So that f_fo[] points to the first bit */

         for (i = begin; i <= end; i++)
            if (f_row[i] != -1)
               f_tabp[pointer + (i - begin)] = f_row[i];
         if (pointer + (end - begin) >= counter)
            counter = pointer + (end - begin + 1);
         while ((f_tabp[first_avail] != -1) && (first_avail <= counter))
            first_avail++;
         }

         /* Write out the arrays. */
         #ifdef DeBugLinker
            if (Dflag)
            fprintf (dbgfile, "\n%ld:\t\t\t\t\t# field offset array\n",
	       (long)pc);
         #endif				/* DeBugLinker */

         /*
          * Compute largest value stored in fo array
          */
         {
	 word maxfo = 0;
         for (i = 0; i < hdr.Nfields; i++) {
            if (f_fo[i] > maxfo) maxfo = f_fo[i];
            }
         if (maxfo < 254)
            hdr.FoffWidth = 1;
         else if (maxfo < 65535L)
            hdr.FoffWidth = 2;
         else
            hdr.FoffWidth = 4;
         }

         for (i = 0; i < hdr.Nfields; i++) {
            #ifdef DeBugLinker
               if (Dflag)
                  fprintf (dbgfile, "\t%d\n", f_fo[i]);
            #endif			/* DeBugLinker */
            if (hdr.FoffWidth == 1) {
	       outchar(f_fo[i]);
	       }
            else if (hdr.FoffWidth == 2)
	       outshort(f_fo[i]);
            else
            outop (f_fo[i]);
            }

         #ifdef DeBugLinker
            if (Dflag)
               fprintf (dbgfile, "\n%ld:\t\t\t\t\t# Bit maps array\n",
	          (long)pc);
         #endif				/* DeBugLinker */

         for (i = 0; i < hdr.Nfields; i++) {
            #ifdef DeBugLinker
               if (Dflag) {
                  int ct, index = i * bytes;
                  unsigned char this_bit = 0200;

                  fprintf (dbgfile, "\t");
                  for (ct = 0; ct < nrecords; ct++) {
                     if ((f_bm[index] | this_bit) == f_bm[index])
                        fprintf (dbgfile, "1");
                     else
                        fprintf (dbgfile, "0");

                     if (ct % 8 == 7) {
                        fprintf (dbgfile, " ");
                        index++;
                        this_bit = 0200;
                        }
                     else
                        this_bit >>= 1;
                     }
                  fprintf (dbgfile, "\n");
                  }
            #endif			/* DeBugLinker */
            for (pointer = i * bytes; pointer < (i + 1) * bytes; pointer++) {
               outchar (f_bm[pointer]);
	       }
            }

         align();

         #ifdef DeBugLinker
            if (Dflag)
               fprintf (dbgfile, "\n%ld:\t\t\t\t\t# record/field array\n",
	          (long)pc);
         #endif				/* DeBugLinker */

         hdr.Ftab = pc;
         for (i = 0; i < counter; i++) {
            #ifdef DeBugLinker
               if (Dflag)
                  fprintf (dbgfile, "\t%d\t%d\n", i, f_tabp[i]);
            #endif			/* DeBugLinker */
            if (hdr.FtabWidth == 1)
               outchar(f_tabp[i]);
            else if (hdr.FtabWidth == 2)
               outshort(f_tabp[i]);
            else
               outop (f_tabp[i]);
            }

         /* Free memory allocated by Jigsaw. */
         free (f_fo);
         free (f_bm);
         free (f_tabp);
         free (f_row);
         }

      #endif				/* FieldTableCompression */

   /*
    * Output descriptors for field names.
    */
   align();
   hdr.Fnames = pc;
   for (fp = lffirst; fp != NULL; fp = fp->f_nextentry) {
      s = &lsspace[fp->f_name];

      #ifdef DeBugLinker
         if (Dflag)
            fprintf(dbgfile, "%ld:\t%d\tS+%ld\t\t\t# %s\n",
               (long)pc, (int)strlen(s), (long)fp->f_name, s);
      #endif				/* DeBugLinker */

      outword(strlen(s));      /* name of field: length & offset */
      outword(fp->f_name);
   }

   /*
    * Output global variable descriptors.
    */
   hdr.Globals = pc;
   for (gp = lgfirst; gp != NULL; gp = gp->g_next) {
      if (gp->g_flag & F_Builtin) {		/* function */
         #ifdef DeBugLinker
            if (Dflag)
               fprintf(dbgfile, "%ld:\t%06lo\t%d\t\t\t# %s\n",
                   (long)pc, (long)D_Proc, -gp->g_procid, &lsspace[gp->g_name]);
         #endif				/* DeBugLinker */
         outword(D_Proc);
         outword(-gp->g_procid);
         }
      else if (gp->g_flag & F_Proc) {		/* Icon procedure */
         #ifdef DeBugLinker
            if (Dflag)
               fprintf(dbgfile, "%ld:\t%06lo\tZ+%ld\t\t\t# %s\n",
                   (long)pc,(long)D_Proc, (long)gp->g_pc, &lsspace[gp->g_name]);
         #endif				/* DeBugLinker */
         outword(D_Proc);
         outword(gp->g_pc);
         }
      else if (gp->g_flag & F_Record) {		/* record constructor */
         #ifdef DeBugLinker
            if (Dflag)
               fprintf(dbgfile, "%ld:\t%06lo\tZ+%ld\t\t\t# %s\n", (long) pc,
                   (long)D_Proc, (long)gp->g_pc, &lsspace[gp->g_name]);
         #endif				/* DeBugLinker */
         outword(D_Proc);
         outword(gp->g_pc);
         }
      else {				/* simple global variable */
         #ifdef DeBugLinker
            if (Dflag)
               fprintf(dbgfile, "%ld:\t%06lo\t0\t\t\t# %s\n",(long)pc,
                  (long)D_Null, &lsspace[gp->g_name]);
         #endif				/* DeBugLinker */
         outword(D_Null);
         outword(0);
         }
      }

   /*
    * Output descriptors for global variable names.
    */
   hdr.Gnames = pc;
   for (gp = lgfirst; gp != NULL; gp = gp->g_next) {

      #ifdef DeBugLinker
         if (Dflag)
            fprintf(dbgfile, "%ld:\t%d\tS+%ld\t\t\t# %s\n",
               (long)pc, (int)strlen(&lsspace[gp->g_name]), (long)(gp->g_name),
                  &lsspace[gp->g_name]);
      #endif				/* DeBugLinker */

      outword(strlen(&lsspace[gp->g_name]));
      outword(gp->g_name);
      }

   /*
    * Output a null descriptor for each static variable.
    */
   hdr.Statics = pc;
   for (i = lstatics; i > 0; i--) {

      #ifdef DeBugLinker
         if (Dflag)
            fprintf(dbgfile, "%ld:\t0\t0\n", (long)pc);
      #endif				/* DeBugLinker */

      outword(D_Null);
      outword(0);
      }
   flushcode();

   /*
    * Output the string constant table and the two tables associating icode
    *  locations with source program locations.  Note that the calls to write
    *  really do all the work.
    */

   hdr.Filenms = pc;
   if (longwrite((char *)fnmtbl, (long)((char *)fnmfree - (char *)fnmtbl),
      outfile) < 0)
         quit("cannot write icode file");

   #ifdef DeBugLinker
      if (Dflag) {
         int k = 0;
         struct ipc_fname *ptr;
         for (ptr = fnmtbl; ptr < fnmfree; ptr++) {
            fprintf(dbgfile, "%ld:\t%03d\tS+%03d\t\t\t# %s\n",
               (long)(pc + k), ptr->ipc, ptr->fname, &lsspace[ptr->fname]);
            k = k + 8;
            }
         putc('\n', dbgfile);
         }

   #endif				/* DeBugLinker */

   pc += (char *)fnmfree - (char *)fnmtbl;

   hdr.linenums = pc;
   if (longwrite((char *)lntable, (long)((char *)lnfree - (char *)lntable),
      outfile) < 0)
         quit("cannot write icode file");

   #ifdef DeBugLinker
      if (Dflag) {
         int k = 0;
         struct ipc_line *ptr;
         for (ptr = lntable; ptr < lnfree; ptr++) {
            fprintf(dbgfile, "%ld:\t%03d\t%03d\n", (long)(pc + k),
               ptr->ipc, ptr->line);
            k = k + 8;
            }
         putc('\n', dbgfile);
         }

   #endif				/* DeBugLinker */

   pc += (char *)lnfree - (char *)lntable;

   hdr.Strcons = pc;
   #ifdef DeBugLinker
      if (Dflag) {
         int c, j, k;
         j = k = 0;
         for (s = lsspace; s < &lsspace[lsfree]; ) {
            fprintf(dbgfile, "%ld:\t%03o", (long)(pc + k), *s++ & 0377);
            k = k + 8;
            for (i = 7; i > 0; i--) {
               if (s >= &lsspace[lsfree])
                  fprintf(dbgfile,"    ");
               else
                  fprintf(dbgfile, " %03o", *s++ & 0377);
               }
            fprintf(dbgfile, "   ");
            for (i = 0; i < 8; i++)
               if (j < lsfree) {
                  c = lsspace[j++];
                  putc(isprint(c & 0377) ? c : ' ', dbgfile);
   	       }
            putc('\n', dbgfile);
            }
         }

   #endif				/* DeBugLinker */

   if (longwrite(lsspace, (long)lsfree, outfile) < 0)
         quit("cannot write icode file");

   pc += lsfree;

   /*
    * Output icode file header.
    */
   hdr.hsize = pc;
   strcpy((char *)hdr.config,IVersion);
   hdr.trace = trace;


   #ifdef DeBugLinker
      if (Dflag) {
         fprintf(dbgfile, "\n");
         fprintf(dbgfile, "size:	 %ld\n", (long)hdr.hsize);
         fprintf(dbgfile, "trace:	 %ld\n", (long)hdr.trace);
         fprintf(dbgfile, "records: %ld\n", (long)hdr.Records);
         fprintf(dbgfile, "ftab:	 %ld\n", (long)hdr.Ftab);
         fprintf(dbgfile, "fnames:  %ld\n", (long)hdr.Fnames);
         fprintf(dbgfile, "globals: %ld\n", (long)hdr.Globals);
         fprintf(dbgfile, "gnames:  %ld\n", (long)hdr.Gnames);
         fprintf(dbgfile, "statics: %ld\n", (long)hdr.Statics);
         fprintf(dbgfile, "strcons:   %ld\n", (long)hdr.Strcons);
         fprintf(dbgfile, "filenms:   %ld\n", (long)hdr.Filenms);
         fprintf(dbgfile, "linenums:   %ld\n", (long)hdr.linenums);
         fprintf(dbgfile, "config:   %s\n", hdr.config);
         }
   #endif				/* DeBugLinker */

   fseek(outfile, hdrsize, 0);
   if (longwrite((char *)&hdr, (long)sizeof(hdr), outfile) < 0)
      quit("cannot write icode file");

   if (verbose >= 2) {
      word tsize = sizeof(hdr) + hdr.hsize;
      fprintf(stderr, "  bootstrap  %7ld\n", hdrsize);
      tsize += hdrsize;
      fprintf(stderr, "  header     %7ld\n", (long)sizeof(hdr));
      fprintf(stderr, "  procedures %7ld\n", (long)hdr.Records);
      fprintf(stderr, "  records    %7ld\n", (long)(hdr.Ftab - hdr.Records));
      fprintf(stderr, "  fields     %7ld\n", (long)(hdr.Globals - hdr.Ftab));
      fprintf(stderr, "  globals    %7ld\n", (long)(hdr.Statics - hdr.Globals));
      fprintf(stderr, "  statics    %7ld\n", (long)(hdr.Filenms - hdr.Statics));
      fprintf(stderr, "  linenums   %7ld\n", (long)(hdr.Strcons - hdr.Filenms));
      fprintf(stderr, "  strings    %7ld\n", (long)(hdr.hsize - hdr.Strcons));
      fprintf(stderr, "  total      %7ld\n", (long)tsize);
      }
   }

/*
 * align() outputs zeroes as padding until pc is a multiple of WordSize.
 */
static void align()
   {
   static word x = 0;

   if (pc % WordSize != 0)
      outblock((char *)&x, (int)(WordSize - (pc % WordSize)));
   }

/*
 * misalign() outputs a Noop instruction for padding if pc + sizeof(int)
 *  is not a multiple of WordSize.  This is for operations that output
 *  an int opcode followed by an operand that needs to be word-aligned.
 */
static void misalign()
   {
   if ((pc + IntBits/ByteBits) % WordSize != 0)
      lemit(Op_Noop, "noop [pad]");
   }

/*
 * intout(i) outputs i as an int that is used by the runtime system
 *  IntBits/ByteBits bytes must be moved from &word[0] to &codep[0].
 */
static void intout(oint)
int oint;
   {
   int i;
   union {
      int i;
      char c[IntBits/ByteBits];
      } u;

   CodeCheck(IntBits/ByteBits);
   u.i = oint;

   for (i = 0; i < IntBits/ByteBits; i++)
      codep[i] = u.c[i];

   codep += IntBits/ByteBits;
   pc += IntBits/ByteBits;
   }

#ifdef FieldTableCompression
/*
 * charout(i) outputs i as an unsigned char that is used by the runtime system
 */
static void charout(unsigned char ochar)
   {
   CodeCheck(1);
   *codep++ = (unsigned char)ochar;
   pc++;
   }
/*
 * shortout(i) outputs i as a short that is used by the runtime system
 *  IntBits/ByteBits bytes must be moved from &word[0] to &codep[0].
 */
static void shortout(short oint)
   {
   int i;
   union {
      short i;
      char c[2];
      } u;

   CodeCheck(2);
   u.i = oint;

   for (i = 0; i < 2; i++)
      codep[i] = u.c[i];

   codep += 2;
   pc += 2;
   }
#endif					/* FieldTableCompression */


/*
 * wordout(i) outputs i as a word that is used by the runtime system
 *  WordSize bytes must be moved from &oword[0] to &codep[0].
 */
static void wordout(oword)
word oword;
   {
   int i;
   union {
        word i;
        char c[WordSize];
        } u;

   CodeCheck(WordSize);
   u.i = oword;

   for (i = 0; i < WordSize; i++)
      codep[i] = u.c[i];

   codep += WordSize;
   pc += WordSize;
   }

/*
 * outblock(a,i) output i bytes starting at address a.
 */
static void outblock(addr,count)
char *addr;
int count;
   {
   CodeCheck(count);
   pc += count;
   while (count--)
      *codep++ = *addr++;
   }

#ifdef DeBugLinker
   /*
    * dumpblock(a,i) dump contents of i bytes at address a, used only
    *  in conjunction with -L.
    */
   static void dumpblock(addr, count)
   char *addr;
   int count;
      {
      int i;
      for (i = 0; i < count; i++) {
         if ((i & 7) == 0)
            fprintf(dbgfile,"\n\t");
         fprintf(dbgfile," %03o",(0377 & (unsigned)addr[i]));
         }
      putc('\n',dbgfile);
      }
   #endif				/* DeBugLinker */

/*
 * flushcode - write buffered code to the output file.
 */
static void flushcode()
   {
   if (codep > codeb)
      if (longwrite(codeb, DiffPtrs(codep,codeb), outfile) < 0)
         quit("cannot write icode file");
   codep = codeb;
   }

/*
 * clearlab - clear label table to all zeroes.
 */
static void clearlab()
   {
   register int i;

   for (i = 0; i < maxlabels; i++)
      labels[i] = 0;
   }

/*
 * backpatch - fill in all forward references to lab.
 */
static void backpatch(lab)
int lab;
   {
   word p, r;
   char *q;
   char *cp, *cr;
   register int j;

   if (lab >= maxlabels)
      labels  = (word *) trealloc(labels, NULL, &maxlabels, sizeof(word),
         lab - maxlabels + 1, "labels");

   p = labels[lab];
   if (p > 0)
      quit("multiply defined label in ucode");
   while (p < 0) {		/* follow reference chain */
      r = pc - (WordSize - p);	/* compute relative offset */
      q = codep - (pc + p);	/* point to word with address */
      cp = (char *) &p;		/* address of integer p       */
      cr = (char *) &r;		/* address of integer r       */
      for (j = 0; j < WordSize; j++) {	  /* move bytes from word pointed to */
         *cp++ = *q;			  /* by q to p, and move bytes from */
         *q++ = *cr++;			  /* r to word pointed to by q */
         }			/* moves integers at arbitrary addresses */
      }
   labels[lab] = pc;
   }

#ifdef DeBugLinker
   void idump(s)		/* dump code region */
      char *s;
      {
      int *c;

      fprintf(stderr,"\ndump of code region %s:\n",s);
      for (c = (int *)codeb; c < (int *)codep; c++)
          fprintf(stderr,"%ld: %d\n",(long)c, (int)*c);
      fflush(stderr);
      }
   #endif				/* DeBugLinker */