-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpass2.c
More file actions
1248 lines (1218 loc) · 30.8 KB
/
Copy pathpass2.c
File metadata and controls
1248 lines (1218 loc) · 30.8 KB
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
/*
pass2.c - Part of llf, a cross linker. Part of the macxx tool chain.
Copyright (C) 2008 David Shepperd
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h> /* get standard I/O definitions */
#include <string.h>
#include <errno.h>
#include <string.h>
#include "token.h" /* define compile time constants */
#include "structs.h" /* define structures */
#include "header.h"
#include "vlda_structs.h"
static struct exp_stk tmp_expr;
int32_t tmp_pool_used;
static char *last_tmp_org;
static int16_t tmp_length;
#if !defined(INLINE)
#define INLINE
#endif
#if ALIGNMENT > 0
#define LAY1(ptr,value) (*ptr.b8++ = (uint8_t)(value))
#define LAY2(ptr,value) do { uint32_t vv=(value); *ptr.b8++ = (char)(vv); *ptr.b8++ = (char)(vv>>8); } while (0)
#define LAY4(ptr,value) do { uint32_t vv=(value); \
*ptr.b8++ = (char)(vv); \
*ptr.b8++ = (char)(vv >>= 8); \
*ptr.b8++ = (char)(vv >>= 8); \
*ptr.b8++ = (char)(vv >>= 8); \
} while (0)
#define LAY8(ptr,value) do { uint64_t vv=(value); \
*ptr.b8++ = (char)(vv); \
*ptr.b8++ = (char)(vv >>= 8); \
*ptr.b8++ = (char)(vv >>= 8); \
*ptr.b8++ = (char)(vv >>= 8); \
*ptr.b8++ = (char)(vv >>= 8); \
*ptr.b8++ = (char)(vv >>= 8); \
*ptr.b8++ = (char)(vv >>= 8); \
*ptr.b8++ = (char)(vv >>= 8); \
} while (0)
#define PICK1(ptr) (*ptr.b8++)
#define PICK2(ptr) (ptr.ub8 += 2, ptr.ub8[-2] | (ptr.ub8[-1] << 8))
#define PICK4(ptr) (ptr.ub8 += 4, ptr.ub8[-4] | (ptr.ub8[-3] << 8) | (ptr.ub8[-2] << 16) | (ptr.ub8[-1] << 24))
#define PICK8(ptr) do { uint64_t vv=0; ptr.ub8 += 7;\
vv = *ptr.ub8; \
vv <<= 8; vv |= *--ptr.ub8; \
vv <<= 8; vv |= *--ptr.ub8; \
vv <<= 8; vv |= *--ptr.ub8; \
vv <<= 8; vv |= *--ptr.ub8; \
vv <<= 8; vv |= *--ptr.ub8; \
vv <<= 8; vv |= *--ptr.ub8; \
vv <<= 8; vv |= *--ptr.ub8; \
} while (0)
#define AMASK ((1<<ALIGNMENT)-1)
#define ALIGN(ptr) (((1<<ALIGNMENT) - ((int)(ptr)&AMASK)) & AMASK)
#else
#define LAY1(ptr,value) *ptr.b8++ = (uint8_t)(value)
#define LAY2(ptr,value) *ptr.b16++ = (uint16_t)(value)
#define LAY4(ptr,value) *ptr.b32++ = (uint32_t)(value)
#define LAY8(ptr,value) *ptr.b64++ = (uint64_t)(value)
#define PICK1(ptr) (*ptr.b8++)
#define PICK2(ptr) (*ptr.b16++)
#define PICK4(ptr) (*ptr.b32++)
#if __SIZEOF_PTRDIFF_T__ > 4
#define PICK8(ptr) (*ptr.b64++)
#endif
#define ALIGN(ptr) 0
#endif
typedef struct tmp_struct
{
uint8_t tf_type;
char tf_tag;
int32_t tfLength;
struct tmp_struct *tfLink;
} TmpStruct_t;
static TmpStruct_t rtmp, *tmp_ptr;
static TmpStruct_t *tmp_next, *tmp_top, *tmp_pool;
static int tmp_pool_size;
static SS_struct *last_segment = 0;
char *sqz_it(char *src, int typ, int32_t cnt, int siz)
{
union
{
char *b8;
int16_t *b16;
int32_t *b32;
#if __SIZEOF_PTRDIFF_T__ > 4
uint64_t *b64;
#endif
int32_t l;
} sqz;
register int32_t value;
register int itz;
sqz.b8 = ((char *)tmp_pool) + 1; /* setup the squeezer */
switch (typ & 0xFF)
{ /* and test for type */
case TMP_EOF:
break; /* this is easy */
case TMP_ASTNG: /* these are strings */
case TMP_BSTNG:
{
itz = cnt * siz;
if ( (value = itz) != 0 )
{
if ( itz < 0 )
value = -itz;
if ( value < 128 )
{
LAY1(sqz, value);
typ |= TMP_B8;
}
else
{
LAY2(sqz, value);
typ |= TMP_B16;
}
}
memcpy(sqz.b8, src, itz); /* move the items */
sqz.b8 += itz; /* adjust the pointer */
break; /* done */
}
case TMP_TAG:
{
LAY1(sqz, *src); /* stuff in the tag character */
if ( (value = cnt) != 0 )
{
if ( cnt < 0 )
value = -cnt; /* get abs(cnt) */
if ( value < 128 )
{
LAY1(sqz, value); /* fits in a byte */
typ |= TMP_B8;
}
else
{
LAY2(sqz, value); /* fits in a word */
typ |= TMP_B16;
}
}
break;
}
case TMP_ORG:
case TMP_EXPR:
case TMP_OOR:
case TMP_BOFF:
case TMP_TEST:
case TMP_START:
{
struct expr_token *texp;
texp = (struct expr_token *)src; /* point to expression area */
typ |= TMP_B8; /* always 1 byte count */
LAY1(sqz, cnt); /* stuff in the item count */
while ( cnt-- )
{ /* do all the elements */
switch (texp->expr_code)
{
case EXPR_OPER:
{
LAY1(sqz, EXPR_OPER | TMP_NNUM);
LAY1(sqz, texp->expr_value);
if ( (char)texp->expr_value == '!' )
{
LAY1(sqz, texp->expr_value >> 8);
}
break;
}
case EXPR_VALUE:
{
uint32_t val;
val = (texp->expr_value >= 0) ? texp->expr_value : -texp->expr_value;
if ( val < 64l )
{
LAY1(sqz, (texp->expr_value & TMP_NUM));
break;
}
if ( val < 128l )
{
LAY1(sqz, EXPR_VALUE | TMP_B8 | TMP_NNUM);
LAY1(sqz, texp->expr_value);
break;
}
if ( val < 32768l )
{
LAY1(sqz, EXPR_VALUE | TMP_B16 | TMP_NNUM);
LAY2(sqz, texp->expr_value);
break;
}
LAY1(sqz, EXPR_VALUE | TMP_B32x | TMP_NNUM);
LAY4(sqz, texp->expr_value);
break;
}
case EXPR_IDENT:
case EXPR_SYM:
{
uint32_t v;
if ( texp->expr_code == EXPR_SYM )
{
LAY1(sqz, EXPR_SYM | TMP_B32x | TMP_NNUM);
#if __SIZEOF_PTRDIFF_T__ == 4
LAY4(sqz, texp->ss_ptr);
#else
LAY8(sqz, texp->ss_ptr);
#endif
}
else
{
v = texp->ss_id;
if ( v < 128l )
{
LAY1(sqz, EXPR_IDENT | TMP_B8 | TMP_NNUM);
LAY1(sqz, v);
}
else if ( v < 32768l )
{
LAY1(sqz, EXPR_IDENT | TMP_B16 | TMP_NNUM);
LAY2(sqz, v);
}
else
{
LAY1(sqz, EXPR_IDENT | TMP_B32x | TMP_NNUM);
LAY4(sqz, v);
}
}
v = (texp->expr_value > 0) ? texp->expr_value : -texp->expr_value;
if ( v < 126 )
{
LAY1(sqz, texp->expr_value);
break;
}
if ( v < 32768 )
{
LAY1(sqz, 126);
LAY2(sqz, texp->expr_value);
break;
}
LAY1(sqz, 127);
LAY4(sqz, texp->expr_value);
break;
} /* -- case */
case EXPR_L:
case EXPR_B:
{
LAY1(sqz, texp->expr_code | TMP_B32x | TMP_NNUM);
#if __SIZEOF_PTRDIFF_T__ == 4
LAY4(sqz, texp->ss_ptr);
#else
LAY8(sqz, texp->ss_ptr);
#endif
} /* -- case */
} /* -- switch expr_code */
++texp;
} /* -- while expr */
break;
} /* -- case TMP_EXPR */
default:
{
sprintf(emsg, "Internal error sqz'ing; Unrecognised TMP code of 0x%02X",
typ);
err_msg(MSG_ERROR, emsg);
}
} /* -- switch TMP_TYPE */
tmp_pool->tf_type = typ; /* stuff in the typ code */
return (sqz.b8);
}
char* unsqz_it(char *src)
{
union
{
uint8_t *ub8;
char *b8;
int16_t *b16;
int32_t *b32;
#if __SIZEOF_PTRDIFF_T__ > 4
uint64_t *b64;
#endif
TmpStruct_t *tsp;
int32_t l;
} sqz;
int typ, i;
tmp_ptr = &rtmp; /* point to local tmp_struct */
rtmp.tfLength = 0; /* assume a length of 0 */
rtmp.tfLink = NULL; /* no no link */
sqz.b8 = src; /* setup the squeezer */
typ = PICK1(sqz);
rtmp.tf_type = typ & ~TMP_SIZE;
switch (rtmp.tf_type)
{ /* and test for type */
default:
{
sprintf(emsg, "Internal error unsqz'ing; Unknown code of %02X",
rtmp.tf_type);
err_msg(MSG_FATAL, emsg);
EXIT_FALSE;
}
case TMP_EOF:
return (sqz.b8); /* this is easy */
case TMP_ASTNG: /* these are strings */
case TMP_BSTNG:
{
typ &= TMP_SIZE;
switch (typ)
{
case TMP_ZERO:
break;
case TMP_B8:
{
rtmp.tfLength = PICK1(sqz);
break;
}
case TMP_B16:
{
rtmp.tfLength = PICK2(sqz);
break;
}
case TMP_B32x:
{
rtmp.tfLength = PICK4(sqz);
break;
}
}
tmp_pool = sqz.tsp;
return (sqz.b8 + rtmp.tfLength);
}
case TMP_TAG:
{
rtmp.tf_tag = PICK1(sqz); /* pickup the tag code */
typ &= TMP_SIZE;
switch (typ)
{
case TMP_ZERO:
break;
case TMP_B8:
{
rtmp.tfLength = PICK1(sqz);
break;
}
case TMP_B16:
{
rtmp.tfLength = PICK2(sqz);
break;
}
case TMP_B32x:
{
rtmp.tfLength = PICK4(sqz);
break;
}
}
return (sqz.b8);
}
case TMP_ORG:
case TMP_EXPR:
case TMP_OOR:
case TMP_BOFF:
case TMP_TEST:
case TMP_START:
{
EXPR_token *texp;
int cnt;
cnt = rtmp.tfLength = PICK1(sqz); /* get the # of elements */
expr_stack_ptr = tmp_expr.len = cnt;
texp = tmp_expr.ptr = expr_stack; /* point to expression stack */
for (; cnt; --cnt, ++texp )
{ /* do all the elements */
i = PICK1(sqz) & 0xFF; /* pickup the type code */
if ( (i & TMP_NNUM) == 0 )
{
texp->expr_code = EXPR_VALUE; /* type is abs value */
if ( i & (TMP_NNUM / 2) )
i |= -TMP_NNUM; /* sign extend */
texp->expr_value = i;
continue;
}
texp->expr_code = i & ~(TMP_SIZE | TMP_NNUM);
i &= TMP_SIZE;
switch (texp->expr_code)
{
case EXPR_OPER:
{
texp->expr_value = PICK1(sqz); /* get the operator character */
if ( texp->expr_value == '!' )
{
texp->expr_value |= PICK1(sqz) << 8;
}
continue;
}
case EXPR_IDENT:
case EXPR_VALUE:
{
switch (i)
{
case TMP_ZERO:
{
texp->expr_value = 0;
break;
}
case TMP_B8:
{
texp->expr_value = PICK1(sqz);
break;
}
case TMP_B16:
{
texp->expr_value = PICK2(sqz);
break;
}
case TMP_B32x:
{
texp->expr_value = PICK4(sqz);
break;
}
}
if ( texp->expr_code != EXPR_IDENT )
continue;
texp->ss_id = texp->expr_value;
/* fall through to EXPR_SYM */
}
case EXPR_SYM:
{
int v;
if ( texp->expr_code == EXPR_SYM )
{ /* may come here from IDENT */
#if __SIZEOF_PTRDIFF_T__ == 4
texp->ss_ptr = (SS_struct *)PICK4(sqz);
#else
texp->ss_ptr = (SS_struct *)PICK8(sqz);
#endif
}
v = PICK1(sqz);
if ( v == 126 )
{
texp->expr_value = PICK2(sqz);
continue;
}
if ( v == 127 )
{
texp->expr_value = PICK4(sqz);
continue;
}
texp->expr_value = v;
continue;
} /* -- case */
case EXPR_L:
case EXPR_B:
{
texp->expr_value = PICK4(sqz);
continue;
} /* -- case */
} /* -- switch expr_code */
} /* -- for() expr */
} /* -- case TMP_EXPR */
} /* -- switch TMP_TYPE */
return (sqz.b8);
}
/********************************************************************
* Write a bunch of data to the temp file
*/
void write_to_tmp(int typ, int32_t itm_cnt, char *itm_ptr, int itm_siz)
/*
* At entry:
* typ - TMP_xxx value id'ing the block data
* itm_cnt - number of items to write
* itm_ptr - pointer to items to write (or tag character)
* itm_siz - size in bytes of each item (or tag number)
* At exit:
* data written to temp file (exits to VMS if error)
*/
{
union
{
char *c;
int32_t *l;
struct tmp_struct *t;
int32_t lng;
} src,dst,tmp; /* mem pointers */
int itz;
#if 0
printf("write_to_tmp(): typ=0x%X(%d), itm_cnt=%d, itm_ptr='%s', itm_siz=%d, OUT_FN_ABS=%s\n",
/ typ, typ, itm_cnt, itm_ptr, itm_siz, output_files[OUT_FN_ABS].fn_present ? "Yes":"No");
#endif
if ( !output_files[OUT_FN_ABS].fn_present )
return; /* nuthin' to do if no ABS wanted */
if ( tmp_pool_size == 0 )
{
tmp_pool_size = MAX_TOKEN * 8; /* get some memory */
tmp_pool_used += MAX_TOKEN * 8;
tmp_pool = (TmpStruct_t *)MEM_alloc(tmp_pool_size);
tmp_top = tmp_pool; /* remember where the top starts */
}
tmp.t = tmp_pool;
itz = (typ == TMP_TAG) ? 0 : itm_cnt * itm_siz;
itz += ALIGN(itz);
if ( tmp_fp == 0 )
{
int tsiz;
tsiz = 2 * sizeof(TmpStruct_t) + itz;
if ( tmp_pool_size < tsiz )
{
if ( tsiz < MAX_TOKEN * 8 )
tsiz = MAX_TOKEN * 8;
tmp.t->tf_type = TMP_LINK; /* link to a new area */
tmp.t->tfLink = (TmpStruct_t *)MEM_alloc(tsiz);
tmp_pool_used += tsiz;
tmp_pool_size = tsiz;
tmp_pool = tmp.t->tfLink;
#if defined(DEBUG_LINK)
printf("Writing TMP_LINK at %p, align=%" FMT_PTRDIF_PRFX "d. New pool at %p, align=%d\n",
(void *)tmp.t, (tmp.t & 3), (void *)tmp_pool, (tmp_pool & 3));
#endif
tmp.t = tmp_pool;
last_tmp_org = NULL;
}
if ( typ == TMP_ORG )
{
if ( last_tmp_org != (char *)0 )
{
tmp_pool_size += tmp.c - last_tmp_org; /* put the bytes back in */
tmp.c = last_tmp_org;
tmp_pool = tmp.t;
}
else
{
last_tmp_org = tmp.c;
}
}
else
{
last_tmp_org = (char *)0;
}
if ( !qual_tbl[QUAL_MISER].present )
{
tmp.t->tf_type = typ; /* set the type */
tmp.t->tfLength = itm_cnt; /* set the item count */
if ( itm_ptr != (char *)0 )
tmp.t->tf_tag = *itm_ptr; /* in case type is tag */
dst.t = tmp.t + 1;
src.c = itm_ptr;
memcpy(dst.c, src.c, itz);
#if 0
if ( typ == TMP_EXPR )
{
EXP_stk tmpExp;
tmpExp.len = itm_cnt;
tmpExp.ptr = (EXPR_token *)itm_ptr;
dump_expr("write_to_tmp before", &tmpExp);
tmpExp.ptr = (EXPR_token *)dst.c;
dump_expr("write_to_tmp after", &tmpExp);
}
#endif
dst.c += itz;
}
else
{
dst.c = sqz_it((char *)itm_ptr, typ, itm_cnt, itm_siz);
}
dst.c += ALIGN(dst.c);
tmp_pool_size -= dst.c - tmp.c;
tmp_pool = dst.t; /* update pointer */
}
else
{
int t;
dst.c = sqz_it(itm_ptr, typ, itm_cnt, itm_siz);
tmp_length = dst.c - (char *)tmp_top;
t = fwrite((char *)&tmp_length, sizeof(tmp_length), 1, tmp_fp);
if ( t != 1 )
{
sprintf(emsg, "Error fwrite'ing %d bytes (1 elem) to \"%s\", wrote %d: %s",
(int)sizeof(tmp_length), output_files[OUT_FN_TMP].fn_buff,
(int)(t * sizeof(tmp_length)), err2str(errno));
err_msg(MSG_FATAL, emsg);
EXIT_FALSE;
}
t = fwrite((char *)tmp_top, (int)tmp_length, 1, tmp_fp);
if ( t != 1 )
{
sprintf(emsg, "Error fwrite'ing %d bytes (1 elem) to \"%s\", wrote %d: %s",
tmp_length, output_files[OUT_FN_TMP].fn_buff,
t * tmp_length, err2str(errno));
err_msg(MSG_FATAL, emsg);
EXIT_FALSE;
}
}
return;
}
/**********************************************************************
* Read a bunch of data from tmp file
*/
int read_from_tmp(void)
/*
* At entry:
* At exit:
*/
{
struct tmp_struct *ts;
char *tmps;
int code;
if ( tmp_fp != 0 )
{
int t;
t = fread((char *)&tmp_length, sizeof(tmp_length), 1, tmp_fp);
if ( t != 1 )
{
sprintf(emsg, "Error fread'ing \"%s\". Wanted %d, got %d: %s",
output_files[OUT_FN_TMP].fn_buff, (int)sizeof(tmp_length),
(int)(t * sizeof(tmp_length)), err2str(errno));
err_msg(MSG_FATAL, emsg);
EXIT_FALSE;
}
t = fread((char *)tmp_top, (int)tmp_length, 1, tmp_fp);
if ( t != 1 )
{
sprintf(emsg, "Error fread'ing \"%s\". Wanted %d, got %d: %s",
output_files[OUT_FN_TMP].fn_buff, tmp_length,
t * tmp_length, err2str(errno));
err_msg(MSG_FATAL, emsg);
EXIT_FALSE;
}
tmps = (char *)unsqz_it((char *)tmp_top); /* unpack the text */
tmps += ALIGN(tmps);
tmp_next = (TmpStruct_t *)tmps;
return (rtmp.tf_type);
}
else
{
ts = tmp_next; /* point to next tmp element */
if ( ts->tf_type == TMP_LINK )
{
int ferr;
#if defined(DEBUG_LINK)
printf("Reading TMP_LINK at %p, align=%" FMT_PTRDIF_PRFX "d. New link at %p, align=%d\n",
(void *)ts, ts & 3, (void *)ts->tfLink, ts->tfLink & 3);
#endif
ts = tmp_next = (TmpStruct_t *)ts->tfLink;
if ( (ferr = MEM_free(tmp_top)) )
{ /* give back the memory */
sprintf(emsg, "Error (%08X) free'ing %d bytes at %p from tmp_pool",
ferr, MAX_TOKEN * 8, (void *)tmp_top);
err_msg(MSG_WARN, emsg);
}
tmp_top = ts; /* point to next top */
}
tmp_ptr = ts; /* point to tmp pointer */
if ( qual_tbl[QUAL_MISER].present )
{
tmps = (char *)unsqz_it((char *)tmp_ptr); /* unpack the text */
tmps += ALIGN(tmps);
#if defined(DEBUG_LINK)
if ( tmp_ptr & 3 )
printf("read_from_tmp: started at unaligned %p\n", (void *)tmp_ptr);
if ( tmps & 3 )
printf("read_from_tmp: ended unaligned at %p\n", (void *)tmps);
#endif
tmp_next = (TmpStruct_t *)tmps;
return (rtmp.tf_type);
}
else
{
++ts;
tmps = (char *)ts;
tmp_pool = ts;
code = tmp_ptr->tf_type;
switch (code)
{
case TMP_TAG:
{
++tmp_next;
break;
}
case TMP_EXPR:
case TMP_START:
case TMP_OOR:
case TMP_BOFF:
case TMP_TEST:
case TMP_ORG:
{
tmp_expr.len = expr_stack_ptr = tmp_ptr->tfLength;
tmp_expr.ptr = (EXPR_token *)ts;
tmps += expr_stack_ptr * sizeof(EXPR_token);
tmp_next = (TmpStruct_t *)tmps;
break;
}
case TMP_BSTNG:
case TMP_ASTNG:
{
tmps += tmp_ptr->tfLength;
tmps += ALIGN(tmps);
tmp_next = (TmpStruct_t *)tmps;
break;
} /* -- case */
case TMP_EOF:
{
break;
}
default:
{
sprintf(emsg, "Internal error: Unrecognised TMP code of %02X",
tmp_ptr->tf_type);
err_msg(MSG_ERROR, emsg);
}
} /* -- switch */
} /* -- if miser */
} /* -- if fp */
return (code);
}
static void rewind_tmp(void)
{
if ( tmp_fp )
{
#ifdef VMS
if ( fseek(tmp_fp, 0, 0) )
{ /* rewind temp file */
sprintf(emsg, "Unable to rewind \"%s\": %s",
output_files[OUT_FN_TMP].fn_buff, err2str(errno));
err_msg(MSG_FATAL, emsg);
EXIT_FALSE;
}
#else
fclose(tmp_fp); /* close the temp file */
if ( (tmp_fp = fopen(output_files[OUT_FN_TMP].fn_buff, "rb")) == 0 )
{
sprintf(emsg, "Error opening tmp file \"%s\" for output: %s",
output_files[OUT_FN_TMP].fn_buff, err2str(errno));
err_msg(MSG_FATAL, emsg);
EXIT_FALSE;
}
#endif
}
else
{
tmp_next = tmp_top;
}
return;
}
static uint32_t pass2_pc = 0;
static void disp_offset(void)
{
char *s1, *s2;
if ( qual_tbl[QUAL_OCTAL].present )
{
s1 = "\t%06lo (%06lo bytes offset from segment {%s} of file %s)\n";
s2 = "\tat location %010lo\n";
}
else
{
s1 = "\t%08lX (%04lX bytes offset from segment {%s} of file %s)\n";
s2 = "\tat location %08lX\n";
}
if ( last_segment )
{
sprintf(emsg, s1,
pass2_pc, pass2_pc - last_segment->ss_value, last_segment->ss_string,
last_segment->ss_fnd->fn_name_only);
}
else
{
sprintf(emsg, s2, pass2_pc);
}
err_msg(MSG_CONT, emsg);
}
/**********************************************************************
* Display truncation error
*/
static void trunc_err(int32_t lowLimit, int32_t highLimit, int32_t written)
/*
* At entry:
* mask - mask to compare against
* token_value - requested value to write
* pass2_pc - current location counter
* last_xxx_ref - points to last seg/sym referenced in expression
*/
{
char *s1,*sign;
if ( lowLimit < 0 )
{
sign = "-";
lowLimit = -lowLimit;
}
else
sign = "";
if ( qual_tbl[QUAL_OCTAL].present )
s1 = "Truncation at location %lo. Expected %s%lo < %lo < %lo. Written: %lo";
else
s1 = "Truncation at location %0lX. Expected %s%lX < %0lX < %0lX. Written: %0lX";
sprintf(emsg, s1, pass2_pc, sign, lowLimit, token_value, highLimit, written);
err_msg(MSG_WARN, emsg);
disp_offset(); /* display error offset */
return;
}
int32_t xfer_addr = 1;
FN_struct *xfer_fnd;
static int noout_flag;
/**********************************************************************
* Pass2 - generate output
*/
int pass2(void)
/*
* At entry:
* no requirements, called from mainline
* At exit:
* output file written.
*/
{
int i, r_flg = 1, flip;
int32_t lc;
#if (0)
char *src,*dst,c;
#endif
uint8_t *ubp;
if ( (outxabs_fp = abs_fp) == 0 )
return (1); /* no output required */
write_to_tmp(TMP_EOF, 0, (char *)0, 0); /* make sure that there's an EOF in tmp */
rewind_tmp(); /* rewind to beginning */
while ( 1 )
{
if ( r_flg )
read_from_tmp(); /* now read the whole thing in */
r_flg = 1; /* gotta read next time */
switch (tmp_ptr->tf_type)
{ /* see what we gotta do */
case TMP_EOF:
{
termobj(xfer_addr); /* terminate and flush the output buffer */
if ( !tmp_fp )
{
int ferr;
if ( (ferr = MEM_free(tmp_top)) )
{ /* give back the memory */
sprintf(emsg, "Error (%08X) free'ing %d bytes at %p from tmp_pool",
ferr, MAX_TOKEN * 8, (void *)tmp_top);
err_msg(MSG_WARN, emsg);
}
tmp_top = 0;
}
return (1); /* done with pass2 */
}
case TMP_OOR:
case TMP_BOFF:
case TMP_TEST:
{
int condit, tmlen, savedType;
char *tmsg;
char *cmd_type;
savedType = tmp_ptr->tf_type;
cmd_type = "TEST";
if ( savedType == TMP_BOFF )
cmd_type = "BOFF";
else if ( savedType == TMP_OOR )
cmd_type = "OOR";
read_from_tmp();
if ( tmp_ptr->tf_type != TMP_ASTNG )
{
sprintf(emsg, "Internal error processing .%s directive", cmd_type);
err_msg(MSG_ERROR, emsg);
r_flg = 0;
break;
}
do
{
tmlen = tmp_ptr->tfLength;
tmsg = (char *)MEM_alloc(tmlen + 1);
strncpy(tmsg, (char *)tmp_pool, tmlen);
*(tmsg + tmlen) = 0; /* null terminate the string */
condit = evaluate_expression(&tmp_expr);
if ( condit )
{
if ( tmp_expr.len == 1 && tmp_expr.ptr->expr_code == EXPR_VALUE )
{
condit = tmp_expr.ptr->expr_value;
}
else
{
if ( qual_tbl[QUAL_REL].present )
{
outtstexp(savedType /*tmp_ptr->tf_type*/, (char *)tmp_pool, (int)tmp_ptr->tfLength, &tmp_expr);
MEM_free(tmsg);
break; /* exit do {} while(); */
}
else
{
sprintf(emsg, "Expression .%s not absolute: %s", cmd_type, tmsg);
err_msg(MSG_ERROR, emsg);
}
condit = 0;
}
}
else
condit = 1;
if ( condit )
{
if ( savedType == TMP_TEST )
{
err_msg(MSG_ERROR, tmsg);
}
else
{
if ( savedType == TMP_BOFF )
{
strcpy(emsg, "Branch offset misaligned or out of range in ");
}
else
{
strcpy(emsg, "Operand misaligned or out of range in ");
}
strcat(emsg, tmsg);
err_msg(MSG_ERROR, emsg);
}
}
MEM_free(tmsg);
break; /* exit do {} while(); */
} while (0);
break; /* Exit switch */
}
case TMP_EXPR:
{
/* dump_expr("read_from_tmp", &tmp_expr); */
if ( noout_flag == 0 )
{
if ( !evaluate_expression(&tmp_expr) )
{
disp_offset();
}
}
read_from_tmp(); /* get the next token */
if ( noout_flag != 0 )
break;
lc = 1; /* assume only 1 thing to output */
i = 4; /* assume type l (LONG, low byte first) */
if ( tmp_ptr->tf_type == TMP_TAG )
{
uint8_t mea_buf[4]; /* for endian-agnostic output */
if ( (lc = tmp_ptr->tfLength) == 0 )
lc = 1; /* get the count */
flip = 0; /* assume not to flip it */
if ( qual_tbl[QUAL_REL].present )
{
if ( tmp_expr.len != 1 || lc != 1
|| tmp_expr.ptr->expr_code != EXPR_VALUE )
{
flushobj(); /* flush the object file */
if ( qual_tbl[QUAL_VLDA].present )
{
union vexp ve;
ve.vexp_chp = eline;
*ve.vexp_type++ = VLDA_EXPR;
outexp(&tmp_expr, ve.vexp_chp, tmp_ptr->tf_tag, lc, eline, abs_fp);
}
else
{
outexp(&tmp_expr, eline, tmp_ptr->tf_tag, lc, eline + 1, abs_fp);
}
break; /* exit the TMP_xxx switch */
}
token_value = tmp_expr.ptr->expr_value;
}
ubp = mea_buf;
switch (tmp_ptr->tf_tag)
{
case 'j':
{ /* long, high byte of low word first */
#if (0)
uint8_t b0,b1,b2,b3; /* bytes in long */
b0 = token_value&0xFF; /* pickup individual bytes */
b1 = (token_value>>8)&0xFF;