-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess.cpp
2288 lines (2056 loc) · 85.6 KB
/
process.cpp
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
#define _CRT_SECURE_NO_WARNINGS
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <cstdarg>
#include <ctime>
#include <cerrno>
#include "main.h"
/////////////////////////////////////////////////////////////////////////////
const uint32_t RAD50_ABS = rad50x2(". ABS."); // ASECT
const uint32_t RAD50_VSEC = rad50x2(". VIR."); // VIRTUAL SECTION SYMBOL NAME
const uint32_t RAD50_VIRSZ = rad50x2("$VIRSZ"); // GLOBAL SYMBOL NAME FOR SIZE OF VIRTUAL SECTION
const uint32_t RAD50_PHNDL = rad50x2("$OHAND"); // OVERLAY HANDLER PSECT
const uint32_t RAD50_ODATA = rad50x2("$ODATA"); // OVERLAY DATA TABLE PSECT
const uint32_t RAD50_PTBL = rad50x2("$OTABL"); // OVERLAY TABLE PSECT
const uint32_t RAD50_GHNDL = rad50x2("$OVRH"); // /O OVERLAY HANDLER GBL ENTRY
const uint32_t RAD50_GVHNDL = rad50x2("$OVRHV"); // /V OVERLAY HANDLER GBL ENTRY
const uint32_t RAD50_GZHNDL = rad50x2("$OVRHZ"); // I-D SPACE OVERLAY HANDLER GBL ENTRY
const uint32_t RAD50_ZTABL = rad50x2("$ZTABL"); // I-D SPACE OVERLAY HANDLER PSECT
static const char* GSDItemTypeNames[] =
{
/*0*/ "MODULE NAME",
/*1*/ "CSECT NAME",
/*2*/ "ISD ENTRY",
/*3*/ "TRANSFER ADDR",
/*4*/ "GLOBAL SYMBOL",
/*5*/ "PSECT NAME",
/*6*/ "IDENT DEFINITION",
/*7*/ "VIRTUAL SECTION"
};
static const char* RLDCommandNames[] =
{
/*000*/ "NOT USED",
/*001*/ "INTERNAL RELOCATION",
/*002*/ "GLOBAL",
/*003*/ "INTERNAL DISPLACED",
/*004*/ "GLOBAL DISPLACED",
/*005*/ "GLOBAL ADDITIVE",
/*006*/ "GLOBAL ADDITIVE DISPLACED",
/*007*/ "LOCATION COUNTER DEFINITION",
/*010*/ "LOCATION COUNTER MODIFICATION",
/*011*/ "SET PROGRAM LIMITS",
/*012*/ "PSECT",
/*013*/ "NOT USED",
/*014*/ "PSECT DISPLACED",
/*015*/ "PSECT ADDITIVE",
/*016*/ "PSECT ADDITIVE DISPLACED",
/*017*/ "COMPLEX",
};
static const char* CPXCommandNames[] =
{
/*000*/ "NOP",
/*001*/ "ADD",
/*002*/ "SUB",
/*003*/ "MUL",
/*004*/ "DIV",
/*005*/ "AND",
/*006*/ "OR",
/*007*/ "XOR",
/*010*/ "NEG",
/*011*/ "COM",
/*012*/ "STORE",
/*013*/ "STORED",
/*014*/ "ILLFMT",
/*015*/ "ILLFMT",
/*016*/ "PUSHGLO",
/*017*/ "PUSHREL",
/*020*/ "PUSHVAL",
};
static const char* weekday_names[] =
{ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
static const char month_names[][4] =
{ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
void prepare_filename(char* buffer, const char* basefilename, const char* extension)
{
assert(buffer != nullptr);
assert(basefilename != nullptr);
assert(*basefilename != 0); // make sure we have the base filename
assert(extension != nullptr);
strcpy(buffer, basefilename);
char* pext = strrchr(buffer, '.');
if (pext == nullptr)
{
pext = buffer + strlen(buffer);
*pext = '.';
}
pext++; // skip the dot
strcpy(pext, extension);
}
/////////////////////////////////////////////////////////////////////////////
// FORCE0 IS CALLED TO GENERATE THE FOLLOWING WARNING MESSAGE, see LINK3\FORCE0
// ?LINK-W-DUPLICATE SYMBOL "SYMBOL" IS FORCED TO THE ROOT
void pass1_force0(const SymbolTableEntry* entry)
{
assert(entry != nullptr);
printf("DUPLICATE SYMBOL \"%s\" IS FORCED TO THE ROOT\n", entry->unrad50name());
}
// LINK3\ORDER, LINK3\ALPHA
void pass1_insert_entry_into_ordered_list(int index, SymbolTableEntry* entry, bool absrel)
{
assert(index > 0 && index < SymbolTableSize);
assert(entry != nullptr);
SymbolTableEntry* sectentry = ASECTentry;
if (Globals.CSECT > 0)
{
sectentry = SymbolTable + Globals.CSECT;
if (absrel && (sectentry->flags() & 0040) != 0)
sectentry = ASECTentry;
}
assert(sectentry != nullptr);
SymbolTableEntry* preventry = sectentry;
bool alphabetize = (Globals.SWITCH & SW_A) != 0;
for (;;)
{
int nextindex = preventry->nextindex();
if (nextindex == 0) // end of chain
break;
SymbolTableEntry* nextentry = SymbolTable + nextindex;
if (nextentry->flagseg & SY_SEC) // next entry is a section
break;
if (alphabetize) // Compare 6-char RAD50 strings
{
if (LOWORD(nextentry->name) > LOWORD(entry->name))
break;
if (LOWORD(nextentry->name) == LOWORD(entry->name) && HIWORD(nextentry->name) > HIWORD(entry->name))
break;
}
else
{
if (nextentry->value > entry->value)
break;
}
preventry = nextentry;
}
// insert the new entry here
int fwdindex = preventry->nextindex();
preventry->status = (uint16_t) ((preventry->status & 0170000) | index);
entry->status = (uint16_t) ((entry->status & 0170000) | fwdindex);
}
// LINK3\TADDR
void process_pass1_gsd_item_taddr(const uint16_t* itemw, const SaveStatusEntry* sscur)
{
assert(itemw != nullptr);
assert(sscur != nullptr);
if ((Globals.BEGBLK.value & 1) == 0) // USE ONLY 1ST EVEN ONE ENCOUNTERED
return; // WE ALREADY HAVE AN EVEN ONE. RETURN
uint32_t lkname = MAKEDWORD(itemw[0], itemw[1]);
uint16_t lkmsk = (uint16_t)~SY_SEC; // CARE ABOUT SECTION FLG
uint16_t lkwd = (uint16_t)SY_SEC; // SECTION NAME LOOKUP IN THE ROOT
int index;
if (!symbol_table_lookup(lkname, lkwd, lkmsk, &index))
fatal_error("ERR31: Transfer address for '%s' undefined or in overlay.\n", unrad50(lkname));
SymbolTableEntry* entry = SymbolTable + index;
printf(" Entry '%s' %06ho %06ho %06ho\n", entry->unrad50name(), entry->flagseg, entry->value, entry->status);
if ((entry->flags() & 040/*CS$REL*/) == 0 ||
(entry->flags() & 4/*CS$ALO*/) != 0 ||
entry->value == 0) // ABS section or OVERLAY SECTION, or CURRENT SIZE IS 0
{
Globals.BEGBLK.symbol = entry->name; // NAME OF THE SECTION
Globals.BEGBLK.flags = entry->flags();
Globals.BEGBLK.code = entry->seg();
Globals.BEGBLK.value = itemw[3];
return;
}
// MUST SCAN CURRENT MODULE TO FIND SIZE CONTRIBUTION TO
// TRANSFER ADDR SECTION TO CALCULATE PROPER OFFSET
size_t offset = 0;
while (offset < sscur->filesize)
{
const uint8_t* data = sscur->data + offset;
uint16_t blocksize = ((uint16_t*)data)[1];
if (blocksize == 0)
break;
uint16_t blocktype = ((uint16_t*)data)[2];
if (blocktype == 1)
{
int itemcounttmp = (blocksize - 6) / 8;
for (int itmp = 0; itmp < itemcounttmp; itmp++)
{
const uint16_t* itemwtmp = (const uint16_t*)(data + 6 + 8 * itmp);
int itemtypetmp = (itemwtmp[2] >> 8) & 0xff;
if ((itemtypetmp == 1/*CSECT*/ || itemtypetmp == 5/*PSECT*/) &&
itemw[0] == itemwtmp[0] && itemw[1] == itemwtmp[1]) // FOUND THE PROPER SECTION
{
uint16_t itemvaltmp = itemwtmp[3];
uint16_t sectsize = (itemvaltmp + 1) & ~1; // ROUND SECTION SIZE TO WORD BOUNDARY
printf(" Item '%s' type %d - CSECT or PSECT size %06ho\n", unrad50(itemwtmp[0], itemwtmp[1]), itemtypetmp, sectsize);
int newvalue = entry->value - sectsize + itemw[3];
//TODO: UPDATE OFFSET VALUE
//TODO
// See LINK3\TADDR\100$
Globals.BEGBLK.symbol = entry->name; // NAME OF THE SECTION
Globals.BEGBLK.flags = entry->flags();
Globals.BEGBLK.code = entry->seg();
Globals.BEGBLK.value = (uint16_t)newvalue /*entry->value*/; // RELATIVE OFFSET FROM THE SECTION
break;
}
}
}
data += blocksize; offset += blocksize;
data += 1; offset += 1; // Skip checksum
}
}
// GLOBAL SYMBOL. See LINK3\SYMNAM
void process_pass1_gsd_item_symnam(const uint16_t* itemw)
{
assert(itemw != nullptr);
uint32_t lkname = MAKEDWORD(itemw[0], itemw[1]);
uint16_t lkwd = 0;
uint16_t lkmsk = (uint16_t)~SY_SEC;
int index;
bool found;
if (itemw[2] & 010/*SY$DEF*/) // IS SYMBOL DEFINED HERE?
{
if (Globals.SW_LML & 0100000) // LIBRARY PREPROCESS PASS?
return; // IGNORE PREPROCESS PASS DEFS
if ((Globals.PAS1_5 & 128) != 0 && Globals.SEGNUM == 0) // ROOT DEF?
{
found = symbol_table_dlooke(lkname, lkwd, lkmsk, &index);
SymbolTableEntry* entry = SymbolTable + index;
if (entry->status & SY_DUP) // IS IT A DUP SYMBOL?
{
symbol_table_delete(index); // DELETE ALL OTHER COPIES OF SYMBOL
fatal_error("ERR70: Duplicate symbol '%s' is defined in non-library.\n", unrad50(lkname));
}
}
else // NOT ROOT, NORMAL LOOKUP
{
found = symbol_table_looke(lkname, lkwd, lkmsk, &index);
}
// LINK3\SYMNAM\100$
if (!found) // LINK3\SYMNAM\140$
{
SymbolTableEntry* entry = SymbolTable + index;
entry->flagseg = (entry->flagseg & ~SY_SEG) | Globals.SEGNUM;
// LINK3\SYMV
entry->value = itemw[3] + Globals.BASE;
pass1_insert_entry_into_ordered_list(index, entry, (itemw[2] & 040) == 0);
}
else // OLD SYMBOL, see LINK3\DEFREF
{
SymbolTableEntry* entry = SymbolTable + index;
if ((entry->status & SY_UDF) == 0) // DEFINED BEFORE?
{
// SYMBOL WAS DEFINED BEFORE, ABSOLUTE SYMBOLS WITH SAME VALUE NOT MULTIPLY DEFINED
if ((itemw[2] & 040/*SY$REL*/) != 0 || entry->value != itemw[3])
warning_message("ERR24: Multiple definition of symbol '%s'.\n", unrad50(lkname));
}
else // DEFINES A REFERENCED SYMBOL, see LINK3\DEFREF\130$
{
symbol_table_remove_undefined(index); // REMOVE ENTRY FROM UNDEFINED LIST
entry->status &= ~(SY_UDF | SY_WK | 07777); // CLR SY.UDF+SY.WK+^CSY.ENB
//TODO: IF INPUT SEG # .NE. SYM SEG # THEN SET EXTERNAL REFERENCE BIT
//TODO: CLEAR SEGMENT # BITS & SET SEGMENT # WHERE DEFINED
entry->value = itemw[3] + Globals.BASE;
pass1_insert_entry_into_ordered_list(index, entry, (itemw[2] & 040) == 0);
}
}
}
else // Symbol referenced here, not defined here
{
if (Globals.SEGNUM == 0) // REFERENCE FROM ROOT?
{
found = symbol_table_dlooke(lkname, lkwd, lkmsk, &index);
SymbolTableEntry* entry = SymbolTable + index;
if (entry->status & SY_DUP) // IS IT A DUP SYMBOL?
{
if ((entry->status & SY_UDF) == 0) // IS SYMBOL DEFINED?
{
symbol_table_delete(index); // DELETE ALL OTHER COPIES OF SYMBOL
pass1_force0(entry);
}
else
{
entry->status |= SY_IND; // EXT. REF.
}
}
}
else // NOT ROOT, NORMAL LOOKUP
{
found = symbol_table_looke(lkname, lkwd, lkmsk, &index);
}
// LINK3\DOREF - PROCESS SYMBOL REFERENCE
if (!found)
{
symbol_table_add_undefined(index);
SymbolTableEntry* entry = SymbolTable + index;
if ((itemw[2] & 1/*SY$WK*/) != 0) // IS REFERENCE WEAK?
entry->status |= SY_WK; // SET WEAK BIT
}
else // Symbol is found
{
SymbolTableEntry* entry = SymbolTable + index;
if ((itemw[2] & 1/*SY$WK*/) == 0) // IS THIS A STRONG REFERENCE?
entry->status &= ~SY_WK; // YES, SO MAKE SURE WEAK BIT IS CLEARED
//TODO: GET SYMBOL FLAG WD & ISOLATE SEGMENT #
//TODO: IF INPUT SEG # .NE. SYM SEG # THEN SET EXTERNAL REFERENCE BIT
if (Globals.SW_LML & 0100000) // ARE WE ON LIB. PREPROCESS PASS?
Globals.FLGWD |= 020/*AD.LML*/; // IND TO ADD TO LML LATER IF LIBRARY CAUSED A NEW UNDEF SYM
}
}
//TODO
}
// LINK3\CSECNM
void process_pass1_gsd_item_csecnm(const uint16_t* itemw, int& itemtype, int& itemflags)
{
assert(itemw != nullptr);
uint32_t lkname = MAKEDWORD(itemw[0], itemw[1]);
//uint16_t lkwd = (uint16_t)SY_SEC;
//uint16_t lkmsk = (uint16_t)~SY_SEC;
if (lkname == 0) // BLANK .CSECT = .PSECT ,LCL,REL,I,CON,RW
{
}
else if (lkname == RAD50_ABS) // Case 2: ASECT
{
//(SWIT1 & SW_J) ?
// .ASECT = .PSECT . ABS.,GBL,ABS,I,OVR,RW (NON I-D SPACE)
// .ASECT = .PSECT . ABS.,GBL,ABS,D,OVR,RW (I-D SPACE)
itemflags |= 0100 + 4; // CS$GBL+CS$ALO == GBL,OVR
if (Globals.SWIT1 & SW_J)
itemtype = 0200; // CS$TYP == D
}
else // Case 3: named section
{
itemflags |= 0100 + 4 + 040; // CS$GBL+CS$ALO+CS$REL == GBL,OVR,REL
}
//TODO: Set flags according to case and process as PSECT
}
// Process GSD item PROGRAM SECTION NAME, see LINK3\PSECNM
void process_pass1_gsd_item_psecnm(uint32_t sectname, int itemflags, uint16_t sectsize)
{
Globals.LRUNUM++; // COUNT SECTIONS IN A MODULE
uint32_t lkname = sectname;
uint16_t lkmsk = (uint16_t)~SY_SEC;
uint16_t lkwd = (uint16_t)SY_SEC;
if (itemflags & 1/*CS$SAV*/) // DOES PSECT HAVE SAVE ATTRIBUTE?
itemflags |= 0100/*CS$GBL*/; // FORCE PSECT TO ROOT VIA GBL ATTRIBUTE
if (itemflags & 0100/*CS$GBL*/) // LOCAL OR GLOBAL SECTION ?
{
lkmsk = (uint16_t)~(SY_SEC + SY_SEG); // CARE ABOUT SECTION FLG & SEGMENT #
lkwd |= Globals.SEGNUM; // LOCAL SECTION QUALIFIED BY SEGMENT #
}
int index;
bool isnewentry = !symbol_table_looke(lkname, lkwd, lkmsk, &index);
SymbolTableEntry* entry = SymbolTable + index;
if (itemflags & 1/*CS$SAV*/) // DOES PSECT HAVE SAV ATTRIBUTE?
entry->status |= SY_SAV; // INDICATE SAV ATTRIBUTE IN PSECT ENTRY
itemflags &= ~(010/*CS$NU*/ | 2/*CS$LIB*/ | 1/*CS$SAV*/); // ALL UNSUPPORTED FLAG BITS
Globals.CSECT = index; // PTR TO SYM TBL ENTRY
if (isnewentry)
{
entry->flagseg |= Globals.SEGNUM; // SET SEGMENT # INDEPEND. OF LCL OR GBL
//TODO: CREATE FORWARD ENTRY # PTR TO NEW NODE
entry->flagseg |= (itemflags << 8);
entry->value = 0; // LENGTH=0 INITIALLY FOR NEW SECTION
}
else // AT THIS POINT SYMBOL WAS ALREADY ENTERED INTO SYMBOL TBL; see LINK3\OLDPCT
{
uint16_t R2 = ((entry->flagseg >> 8) & 0377) // GET PSECT FLAG BITS IN R2
& ~(010/*CS$NU*/ | 2/*CS$LIB*/ | 1/*CS$SAV*/); // GET RID OF UNUSED FLAG BITS
if (Globals.SWIT1 & SW_J) // ARE WE PROCESSING I-D SPACE?
{
//TODO
NOTIMPLEMENTED
}
if (R2 != itemflags) // ARE SECTION ATTRIBUTES THE SAME?
fatal_error("ERR10: Conflicting section '%s' attributes. R=0x%x flags=0x%x\n", entry->unrad50name(), R2, itemflags);
}
// CHKRT
if ((entry->flags() & 0400/*CS$GBL*/) != 0 && entry->seg() != 0)
{
NOTIMPLEMENTED;
}
// PLOOP and CHKRT
if (lkname == RAD50_ABS)
ASECTentry = entry;
else if (isnewentry)
{
SymbolTableEntry* pSect = ASECTentry;
while (pSect != nullptr) // find section chain end
{
int nextindex = (pSect->status & 07777);
if (nextindex == 0) // end of chain
break;
pSect = SymbolTable + nextindex;
}
if (pSect != nullptr)
{
//int sectindex = pSect - SymbolTable;
pSect->status |= index; // set link to the new segment
}
}
//TODO: primitive version, depends on section flags
//if ((itemflags & 0040/*CS$REL*/) == 0 || (Globals.SWITCH & SW_X) != 0)
// sectsize = 0;
if ((itemflags & 0200/*CS$TYP*/) == 0 || (itemflags & 0004/*CS$ALO*/) != 0) // INSTRUCTION SECTION? CON SECTION ?
sectsize = (sectsize + 1) & ~1; // ROUND SECTION SIZE TO WORD BOUNDARY
if ((itemflags & 0040/*CS$REL*/) == 0) // ASECT
{
if (sectsize > entry->value) // Size is maximum from all ASECT sections
entry->value = sectsize;
Globals.BASE = 0;
}
else if (itemflags & 4/*CS$ALO*/) // OVERLAYED SECTION?
{
Globals.BASE = 0; // OVR PSECT, GBL SYM OFFSET IS FROM START OF SECTION
if (sectsize > entry->value) // Size is maximum from all such sections
entry->value = sectsize;
}
else
{
Globals.BASE = entry->value;
entry->value += sectsize;
}
}
void process_pass1_gsd_item_psecnm(const uint16_t* itemw, int itemflags)
{
assert(itemw != nullptr);
uint32_t sectname = MAKEDWORD(itemw[0], itemw[1]);
uint16_t sectsize = itemw[3];
process_pass1_gsd_item_psecnm(sectname, itemflags, sectsize);
}
// PROCESS GSD TYPES, see LINK3\GSD
void process_pass1_gsd_item(const uint16_t* itemw, const SaveStatusEntry* sscur)
{
assert(itemw != nullptr);
assert(sscur != nullptr);
uint32_t itemnamerad50 = MAKEDWORD(itemw[0], itemw[1]);
int itemtype = (itemw[2] >> 8) & 0xff;
int itemflags = (itemw[2] & 0377);
printf(" Item '%s' type %d - %s", unrad50(itemnamerad50), itemtype, (itemtype > 7) ? "UNKNOWN" : GSDItemTypeNames[itemtype]);
switch (itemtype)
{
case 0: // 0 - MODULE NAME FROM .TITLE, see LINK3\MODNME
printf(", base %06ho\n", Globals.BASE);
if (Globals.MODNAM == 0)
Globals.MODNAM = itemnamerad50;
break;
case 2: // 2 - ISD ENTRY (IGNORED), see LINK3\ISDNAM
printf(", ignored\n");
break;
case 3: // 3 - TRANSFER ADDRESS; see LINK3\TADDR
printf(" %06ho\n", itemw[3]);
process_pass1_gsd_item_taddr(itemw, sscur);
break;
case 4: // 4 - GLOBAL SYMBOL, see LINK3\SYMNAM
printf(" flags %03o addr %06ho\n", itemflags, itemw[3]);
process_pass1_gsd_item_symnam(itemw);
break;
case 1: // 1 - CSECT NAME, see LINK3\CSECNM
printf(" %06ho\n", itemw[3]);
process_pass1_gsd_item_csecnm(itemw, itemtype, itemflags);
process_pass1_gsd_item_psecnm(itemw, itemflags);
break;
case 5: // 5 - PSECT NAME; see LINK3\PSECNM
printf(" flags %03o maxlen %06ho\n", itemflags, itemw[3]);
process_pass1_gsd_item_psecnm(itemw, itemflags);
break;
case 6: // 6 - IDENT DEFINITION; see LINK3\PGMIDN
println();
if (Globals.IDENT == 0)
Globals.IDENT = itemnamerad50;
break;
case 7: // 7 - VIRTUAL SECTION; see LINK3\VSECNM
{
printf(" size %06ho\n", itemw[3]);
uint16_t sectsize = itemw[3];
// A VIRTUAL SECTION HAS BEEN FOUND WHICH IS PROCESSED SIMILAR TO
// .PSECT . VIR.,RW,D,GBL,REL,CON
// THE SIZE IS THE NUMBER OF 32 WORD AREAS REQUIRED. THE SEGMENT # IS DEFINED AS THE ROOT (ZERO).
// THERE WILL NEVER BE ANY GLOBALS UNDER THIS SECTION AND THE SECTION STARTS AT A BASE OF ZERO.
itemflags = 0200/*CS$TYP*/ | 0100/*CS$GBL*/ | 040/*CS$REL*/;
process_pass1_gsd_item_psecnm(RAD50_VSEC, itemflags, 0);
// IF $VIRSZ ALREADY IN SYM TBL THEN JUST ADD SIZE OF CURRENT VSECT TO IT
// ELSE ENTER NEW SYMBOL "$VIRSZ" WITH VALUE EQUAL TO CURRENT SIZE.
uint32_t lkname = RAD50_VIRSZ;
uint16_t lkmsk = (uint16_t)~SY_SEC;
uint16_t lkwd = 0;
int index;
bool isnewentry = !symbol_table_looke(lkname, lkwd, lkmsk, &index);
SymbolTableEntry* entry = SymbolTable + index;
if (isnewentry)
{
entry->flagseg = 010/*SY$DEF*/ | (4/*GLOBAL SYMBOL*/ << 8);
entry->value = sectsize;
pass1_insert_entry_into_ordered_list(index, entry, true);
}
else
{
entry->value += Globals.BASE;
}
}
break;
default:
println();
fatal_error("ERR21: Bad GSD type %d found in %s.\n", itemtype, sscur->filename);
}
}
void process_pass1_gsd_block(const SaveStatusEntry* sscur, const uint8_t* data)
{
assert(sscur != nullptr);
assert(data != nullptr);
uint16_t blocksize = ((uint16_t*)data)[1];
int itemcount = (blocksize - 6) / 8;
//printf(" Processing GSD block, %d items\n", itemcount);
for (int i = 0; i < itemcount; i++)
{
const uint16_t* itemw = (const uint16_t*)(data + 6 + 8 * i);
memcpy(Globals.TXTBLK, itemw, 8);
process_pass1_gsd_item(itemw, sscur);
}
}
void process_pass1_file(SaveStatusEntry* sscur)
{
assert(sscur != nullptr);
assert(sscur->data != nullptr);
printf(" Processing %s\n", sscur->filename);
size_t offset = 0;
while (offset < sscur->filesize - 1)
{
uint8_t* data = sscur->data + offset;
uint16_t* dataw = (uint16_t*)(data);
if (*dataw != 1)
{
if (*dataw == 0) // Possibly that is filler at the end of the block
{
size_t offsetnext = (offset + 511) & ~511;
while (offset < sscur->filesize && offset < offsetnext)
{
if (*data != 0) break;
data++; offset++;
}
if (offset >= sscur->filesize)
break; // End of file
}
dataw = (uint16_t*)(data);
if (*dataw != 1)
fatal_error("Unexpected word %06ho at %06o in %s\n", *dataw, (unsigned int)offset, sscur->filename);
}
uint16_t blocksize = ((uint16_t*)data)[1];
uint16_t blocktype = ((uint16_t*)data)[2];
if (blocktype == 0 || blocktype > 8)
fatal_error("Illegal record type at %06o in %s\n", (unsigned int)offset, sscur->filename);
else if (blocktype == 1) // 1 - START GSD RECORD, see LINK3\GSD
{
printf(" Block type 1 - GSD at %06o size %06ho\n", (unsigned int)offset, blocksize);
process_pass1_gsd_block(sscur, data);
}
else if (blocktype == 6) // 6 - MODULE END, see LINK3\MODND
{
//printf(" Block type 6 - ENDMOD at %06o size %06ho\n", (unsigned int)offset, blocksize);
if (Globals.HGHLIM < Globals.LRUNUM)
Globals.HGHLIM = Globals.LRUNUM;
Globals.LRUNUM = 0;
//TODO
}
else if (blocktype == 7) // See LINK3\LIBRA
{
uint16_t libver = ((uint16_t*)data)[3];
if (libver < L_HVER)
fatal_error("ERR23: Old library format (%03ho) in %s\n", libver, sscur->filename);
sscur->islibrary = true;
Globals.PAS1_5 |= 1; // ALSO SAY LIBRARY FILE PROCESSING REQUIRED
break; // Skipping library files on Pass 1
}
data += blocksize; offset += blocksize;
data += 1; offset += 1; // Skip checksum
}
}
// PASS1: GSD PROCESSING, see LINK3\PASS1
void process_pass1()
{
// PROCESS FORMATTED BINARY RECORDS, see LINK3\PA1
for (int i = 0; i < SaveStatusCount; i++)
{
SaveStatusEntry* sscur = SaveStatusArea + i;
process_pass1_file(sscur);
}
}
// SEARCH THE ENTRY POINT TABLE FOR A MATCH OF THE INDICATED SYMBOL. See LINK3\EPTSER
const uint16_t* process_pass15_eptsearch(const uint8_t* data, uint16_t eptsize, uint32_t symbol)
{
assert(data != nullptr);
for (uint16_t eptno = 0; eptno < eptsize; eptno++)
{
const uint16_t* itemw = (uint16_t*)(data + 8 * eptno);
if (itemw[0] == 0)
break; // IF 0 AT END OF TBL
if (itemw[2] == 0)
continue; // SKIP MODULE NAMES IN EPT, THEY ARE NOT VALID ENTRY POINTS
uint32_t itemnamerad50 = MAKEDWORD(itemw[0], itemw[1]);
if (itemnamerad50 == symbol) // MATCH
return itemw;
}
return nullptr;
}
// PLACE THE ADDR OF A LIBR SYMBOL INTO THE LML. See LINK3\LMLBLD
void process_pass15_lmlbld(uint16_t blockno, uint16_t offset)
{
if (LibraryModuleCount >= LibraryModuleListSize)
fatal_error("ERR22: Library list overflow.\n");
// SEARCH ALL PREVIOUS LML'S FOR A DUPLICATE
for (int i = 0; i < LibraryModuleCount; i++)
{
const LibraryModuleEntry* lmlentry0 = LibraryModuleList + i;
if (lmlentry0->libfileno == Globals.LIBNB &&
lmlentry0->relblockno == (blockno & 077777) && lmlentry0->byteoffset == offset)
return; // MATCH, DON'T ADD LML TO LIST
}
LibraryModuleEntry* lmlentry = LibraryModuleList + LibraryModuleCount;
lmlentry->libfileno = Globals.LIBNB;
lmlentry->relblockno = blockno & 077777;
lmlentry->byteoffset = offset;
lmlentry->passno = 0;
lmlentry->segmentno = 0; // SET SEGMNT NUMBER TO ZERO=ROOT
//TODO
LibraryModuleCount++;
}
// ORDER LIBRARY MODULE LIST. See LINK3\ORLIB
void process_pass15_lmlorder()
{
if (LibraryModuleCount < 2)
return; // NOTHING TO ORDER. RETURN
// Find where to start - first not processed
int kbase = LibraryModuleCount;
for (int k = 0; k < LibraryModuleCount; k++)
{
const LibraryModuleEntry* ek = LibraryModuleList + k;
if (ek->passno == 0)
{
kbase = k;
break;
}
}
if (kbase == LibraryModuleCount)
return; // Nothing to order
// Sort from kbase to the end of the array
for (int k = kbase + 1; k < LibraryModuleCount; k++)
{
const LibraryModuleEntry* ek = LibraryModuleList + k;
for (int j = kbase; j < k; j++)
{
LibraryModuleEntry* ej = LibraryModuleList + j;
if (ek->libfileno < ej->libfileno ||
(ek->libfileno == ej->libfileno && ek->offset() < ej->offset()))
{
LibraryModuleEntry etemp = *ek;
memmove(ej + 1, ej, sizeof(LibraryModuleEntry) * (k - j));
*ej = etemp;
}
}
}
}
// READ MODULES FROM THE LIBRARY. See LINK3\LIBPRO
void process_pass15_libpro(const SaveStatusEntry* sscur)
{
assert(sscur != nullptr);
assert(sscur->data != nullptr);
Globals.DUPMOD = 0; // ASSUME THIS IS NOT A DUP MOD
//printf(" process_pass15_libpro() for library #%d %s\n", (int)Globals.LIBNB, sscur->filename);
size_t offset = 0;
for (int i = 0; i < LibraryModuleCount; i++)
{
LibraryModuleEntry* lmlentry = LibraryModuleList + i;
if (lmlentry->libfileno != Globals.LIBNB)
continue; // not this library file
if (lmlentry->offset() == offset)
continue; // same offset
if (lmlentry->passno > 0)
continue; // already passed
offset = lmlentry->offset();
printf(" Module #%d offset %06o\n", i, (unsigned int)offset);
while (offset < sscur->filesize)
{
uint8_t* data = sscur->data + offset;
//uint16_t* dataw = (uint16_t*)(data);
uint16_t blocksize = ((uint16_t*)data)[1];
uint16_t blocktype = ((uint16_t*)data)[2];
if (blocktype == 0 || blocktype > 8)
fatal_error("Illegal record type at %06o in %s\n", (unsigned int)offset, sscur->filename);
else if (blocktype == 1) // 1 - START GSD RECORD, see LINK3\GSD
{
printf(" Block type 1 - GSD at %06o size %06ho\n", (unsigned int)offset, blocksize);
process_pass1_gsd_block(sscur, data);
}
else if (blocktype == 6) // 6 - MODULE END, see LINK3\MODND
{
//printf(" Block type 6 - ENDMOD at %06o size %06ho\n", (unsigned int)offset, blocksize);
if (Globals.HGHLIM < Globals.LRUNUM)
Globals.HGHLIM = Globals.LRUNUM;
Globals.LRUNUM = 0;
break;
}
data += blocksize; offset += blocksize;
data += 1; offset += 1; // Skip checksum
}
lmlentry->passno++; // Mark as passed
}
}
void process_pass15_library(const SaveStatusEntry* sscur)
{
size_t offset = 0;
while (offset < sscur->filesize - 1)
{
uint8_t* data = sscur->data + offset;
uint16_t* dataw = (uint16_t*)(data);
if (*dataw != 1)
{
// Possibly that is filler at the end of the block
size_t offsetnext = (offset + 511) & ~511;
size_t shift = offsetnext - offset;
data += shift; offset += shift;
if (offset >= sscur->filesize)
break; // End of file
dataw = (uint16_t*)(data);
if (*dataw != 1)
#define IGNORE_BROKEN_ENTRIES 1
#ifndef IGNORE_BROKEN_ENTRIES
fatal_error("Unexpected word %06ho at %06o in %s\n", *dataw, (unsigned int)offset, sscur->filename);
#else
{
warning_message("Unexpected word %06ho at %06o in %s. Trying skip to ENDLIB\n", *dataw, (unsigned int)offset, sscur->filename);
while ((dataw[0] != 1 || dataw[1] != 6 || dataw[2] != 6) && offset < sscur->filesize)
{
dataw++;
data += 2;
offset += 2;
}
if (*dataw != 1)
{
fatal_error("Unable to find ENDLIB till offset %06o in %s\n", *dataw, (unsigned int)offset, sscur->filename);
}
}
#endif
}
uint16_t blocksize = ((uint16_t*)data)[1];
uint16_t blocktype = ((uint16_t*)data)[2];
if (blocktype == 0 || blocktype > 8)
fatal_error("Illegal record type %03ho at %06o in %s\n", blocktype, (unsigned int)offset, sscur->filename);
if (blocktype == 7) // See LINK3\LIBRA, WE ARE ON PASS 1.5 , SO PROCESS LIBRARIES
{
printf(" Block type 7 - TITLIB at %06o size %06ho\n", (unsigned int)offset, blocksize);
uint16_t eptsize = *(uint16_t*)(data + L_HEAB);
printf(" EPT size %06ho bytes, %d. records\n", eptsize, (int)(eptsize / 8));
//Globals.SVLML = Globals.STLML; // SAVE START OF ALL LML'S FOR THIS LIB
Globals.FLGWD |= LB_OBJ; // IND LIB FILE TO LINKER
//TODO: R4 -> 1ST WD OF BUFR & C=0
if (Globals.SWITCH & SW_I) // ANY /I MODULES?
Globals.FLGWD |= FG_IP; // SET FLAG INDICATING /I PASS FIRST
if (Globals.SW_LML) // IS SW.LML SET?
Globals.SW_LML |= 0100000; // MAKE SURE BIT IS SET, /I TURNS IT OFF
//Globals.ESZRBA = *(uint16_t*)(data + L_HEAB); // SIZE OF EPT IN BYTES
Globals.SEGBAS = 0; // SEGBAS->TEMP FOR /X LIB FLAG
//Globals.ESZRBA = Globals.ESZRBA >> 1; // NOW WORDS
if (*(uint16_t*)(data + L_HX)) // IS /X SWITCH SET IN LIB. HEADER?
{
Globals.SW_LML &= ~0100000; // NO PREPROCESSING ON /X LIBRARIES
Globals.SEGBAS++; // /X LIBRARY ->SEGBAS=1
//TODO: WILL /X EPT FIT IN BUFFER?
}
data += L_HEPT; offset += L_HEPT; // Move to 1ST EPT ENTRY
// Resolve undefined symbols using EPT
if (is_any_undefined()) // IF NO UNDEFS, THEN END LIBRARY
{
int index = Globals.UNDLST;
while (index > 0)
{
SymbolTableEntry* entry = SymbolTable + index;
if ((entry->status & SY_WK) != 0) // IS THIS A WEAK SYMBOL? SKIP WEAK SYMBOL
{
index = entry->nextindex();
continue;
}
if ((entry->status & SY_DUP) != 0) // IS SYMBOL A DUP SYMBOL?
{
//TODO: IS SEG NUM FIELD ZERO?
index = entry->nextindex();
continue;
}
const uint16_t* itemw = process_pass15_eptsearch(data, eptsize, entry->name);
if (itemw == nullptr) // CONTINUE THRU UNDEF LIST
{
index = entry->nextindex();
continue;
}
// THE UNDEFINED SYMBOL HAS BEEN FOUND IN THE CURRENT ENTRY POINT TABLE.
uint16_t eptblock = itemw[2];
uint16_t eptoffset = itemw[3] & 0777;
printf(" Found EPT for '%s' block %06ho offset %06ho\n", entry->unrad50name(), eptblock, eptoffset);
// CALL LMLBLD - PLACE MOD ADR IN LML
process_pass15_lmlbld(eptblock, eptoffset);
//TODO: IS THIS A /I SYMBOL
const uint16_t* itemwmod = itemw;
for (;;) // GO SEARCH FOR MODULE NAME
{
itemwmod -= 4; // BACK ONE EPT
if (itemwmod <= (uint16_t*)data)
break;
if (itemwmod[2] & 0x8000) // MODULE NAME?
break;
}
const uint16_t* itemw1 = itemwmod;
itemw1 += 4; // GO TO NEXT SYMBOL
//TODO
index = entry->nextindex(); // CONTINUE THRU UNDEF LIST
}
}
data += eptsize; offset += eptsize;
continue;
}
else if (blocktype == 8) // LINK3\ENDLIB
{
printf(" Block type 10 - ENDLIB at %06o size %06ho\n", (unsigned int)offset, blocksize);
process_pass15_lmlorder(); // ORDER THIS LIBRARY LML
//print_lml_table();//DEBUG
Globals.SW_LML &= ~0100000; // RESET FOR FINAL OBJ. PROCESSING
Globals.FLGWD &= ~AD_LML; // CLEAR NEW UNDF FLAG
// READ MODULES FROM THE LIBRARY
process_pass15_libpro(sscur);
}
data += blocksize; offset += blocksize;
data += 1; offset += 1; // Skip checksum
}
}
// PASS 1.5 SCANS ONLY LIBRARIES
void process_pass15()
{
Globals.LIBNB = 0; // RESET LIBRARY FILE #
for (int i = 0; i < SaveStatusCount; i++)
{
SaveStatusEntry* sscur = SaveStatusArea + i;
assert(sscur->data != nullptr);
// Skipping non-library files on Pass 1.5
if (!sscur->islibrary) // SKIP ALL NON-LIBRARY FILES ON LIBRARY PASS
continue;
Globals.LIBNB++; // BUMP LIBRARY FILE # FOR LML
for (int j = 1; ; j++)
{
Globals.FLGWD &= ~AD_LML; // CLEAR NEW UNDF FLAG
printf(" Processing %s (%d), %06ho\n", sscur->filename, j, Globals.BASE);
process_pass15_library(sscur);
if ((Globals.FLGWD & AD_LML) == 0) // NEW UNDEF'S ADDED WHILE PROCESSING LIBR ?
break;
}
}
// Process weak symbols, see LINK3\PASS1\30$
if (is_any_undefined()) // ARE THERE ANY UNDEFINED GLOBALS LEFT? IF NO then NO WEAK SYMBOLS LEFT
{
int index = Globals.UNDLST;
while (index > 0)
{
SymbolTableEntry* entry = SymbolTable + index;
uint16_t nextindex = entry->nextindex();
if ((entry->status & SY_WK) != 0) // GET A WEAK SYMBOL FROM THE UNDEFINED SYMBOL TABLE LIST
{
// WE HAVE A WEAK SYMBOL. NOW DEFINE IT WITH ABS VALUE OF 0
Globals.CSECT = 0; // ALL WEAK SYMBOLS GO IN . ABS. PSECT
Globals.BASE = 0; // CLEAR BASE SINCE WE ARE IN .ABS PSECT
symbol_table_remove_undefined(index); // REMOVE ENTRY FROM UNDEFINED LIST
entry->status &= ~(SY_UDF | SY_WK | 07777); // CLR SY.UDF+SY.WK+^CSY.ENB
entry->flagseg = 4 * 0400 | 010/*SY$DEF*/; // PUT ENTRY TYPE AND FLAG BYTES IN ENTRY
//TODO: IF INPUT SEG # .NE. SYM SEG # THEN SET EXTERNAL REFERENCE BIT
//TODO: CLEAR SEGMENT # BITS & SET SEGMENT # WHERE DEFINED
entry->value = 0; // VALUE WORD IS 0
pass1_insert_entry_into_ordered_list(index, entry, true);
//print_symbol_table();//DEBUG
}
index = nextindex;
}
}
}
// END PASS ONE PROCESSING; see LINK4\ENDP1
void process_pass1_endp1()
{
//TODO
{
// NOW LOOKUP IN SYMBOL TABLE FOR THE VIRTUAL SECTION, IF IT IS THERE
// ZERO IT'S VALUE WORD(INDICATES SIZE OF SECTION) SO DOES NOT
// CONTRIBUTE TO SIZE OF PROGRAM DURING MAP PASS
uint16_t lkwd = (uint16_t)SY_SEC; // SECTION NAME LOOKUP
uint16_t lkmsk = (uint16_t)~SY_SEC; // CARE ABOUT SECTION FLG
int index;
if (symbol_table_lookup(RAD50_VSEC, lkwd, lkmsk, &index))
{
SymbolTableEntry* entry = SymbolTable + index;
entry->value = 0;
}
}
if (ASECTentry == nullptr) //HACK: add default ABS section if we still don't have one
{
// Enter ABS section into the symbol table
uint16_t lkwd = (uint16_t)SY_SEC;
uint16_t lkmsk = (uint16_t)~SY_SEC;
int index;
symbol_table_dlooke(RAD50_ABS, lkwd, lkmsk, &index);