-
Notifications
You must be signed in to change notification settings - Fork 1
/
pass1.c
1898 lines (1849 loc) · 58.2 KB
/
pass1.c
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
/*
pass1.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 <ctype.h> /* get standard string type macros */
#include <string.h>
#include <errno.h>
#include "token.h" /* define compile time constants */
#include "structs.h" /* define structures */
#include "header.h" /* get our standard stuff */
#include "exproper.h" /* expression operators */
#include "add_defs.h"
#undef NULL
#define NULL 0
#if 0
extern long id_table_size;
extern FN_struct *xfer_fnd;
extern struct fn_struct *get_fn_pool();
#endif
char *target; /* pointer to target string */
char emsg[512]; /* space to build error messages */
char *inp_str; /* place to hold input text */
int inp_str_size;
char *inp_ptr=0; /* pointer to next place to get token */
char *tkn_ptr=0; /* pointer to start of token */
long token_value; /* value of converted token */
int token_type; /* token type */
char token_end; /* terminator char for string tokens */
char token_curchr; /* current token character being processed */
int token_minus; /* a minus as been detected */
char *token_pool=0; /* pointer to free token memory */
int token_pool_size; /* size of remaining free token memory */
struct expr_token expr_stack[32]; /* expression stack */
int expr_stack_ptr; /* size of current expression stack */
int cmd_code; /* command code */
struct seg_spec_struct *seg_spec_pool=0; /* pointer to free segment space */
DBG_seclist *dbg_sec_pool=0;
int seg_spec_size; /* size of segment pool */
/******************************************************************
* Pick up memory for special segment block storage
*/
SEG_spec_struct *get_seg_spec_mem( SS_struct *seg_ptr)
/* At entry:
* seg_ptr - pointer to segment block to extend
* At exit:
* seg_spec_pool is updated to point to new free space
* seg_spec_size is updated to reflect the size of seg_spec_pool
*/
{
if (--seg_spec_size < 0)
{
int t = sizeof(struct seg_spec_struct)*32;
sym_pool_used += t;
seg_spec_pool = (struct seg_spec_struct *)MEM_alloc(t);
dbg_sec_pool = (DBG_seclist *)MEM_alloc(t);
seg_spec_size = 31;
}
seg_ptr->flg_segment = 1;
if (!seg_ptr->flg_group)
{
if (current_fnd->od_seclist_curr != 0)
{
current_fnd->od_seclist_curr->next = dbg_sec_pool;
}
current_fnd->od_seclist_curr = dbg_sec_pool++;
if (current_fnd->od_seclist_top == 0)
{
current_fnd->od_seclist_top = current_fnd->od_seclist_curr;
}
current_fnd->od_seclist_curr->segp = seg_ptr;
}
return(seg_ptr->seg_spec = seg_spec_pool++);
}
/******************************************************************/
long record_count;
/******************************************************************
* Pick up a line of text from the input file
*/
int get_text( void )
/*
* At entry:
* No requirements.
* At exit:
* inp_str is loaded with next input line.
* if EOF detected, routine returns with EOF else
* routine returns TRUE
*/
{
char *s;
int len;
if (token_pool_size <= MAX_TOKEN)
{
token_pool_size = MAX_TOKEN*8; /* get a bunch of memory */
token_pool = MEM_alloc(token_pool_size); /* pick up some garbage area */
misc_pool_used += token_pool_size;
}
s = inp_str;
if (!fgets(inp_ptr=s,inp_str_size-3,current_fnd->fn_file))
{
*inp_ptr = '\0';
if (fgetc(current_fnd->fn_file) == EOF) return(EOF);
sprintf(emsg,"Error reading input from \"%s\": %s",
current_fnd->fn_buff,err2str(errno));
err_msg(MSG_FATAL,emsg);
EXIT_FALSE;
}
len = strlen(s);
s += len;
while (*(s-1) != '\n')
{
inp_str_size += inp_str_size/2;
inp_ptr = inp_str = (char *)MEM_realloc(inp_str, inp_str_size);
s = inp_str+len;
if (!fgets(s, inp_str_size-len-3, current_fnd->fn_file))
{
*s++ = '\\';
*s++ = '\n';
*s = 0;
break;
}
len += strlen(s);
s += len;
}
record_count++;
if (option_input) puts_map(inp_str,-1);
return(1);
}
/******************************************************************/
/*****************************************************************
* Get a character from inp_str.
*/
int get_c( void )
/*
* At entry:
* inp_str is assumed to contain the source line
* inp_ptr is assumed to point to the next source character
* At exit:
* routine returns with next source character. inp_ptr is
* updated to reflect change. A new source line may have been
* read in if the previous line ended with '\'. If EOF is
* detected, routine returns with EOF instead of char.
*/
{
char c;
do
{
if ((c= *inp_ptr++) != '\\')
{
if (c) return c;
inp_ptr--;
return(c);
}
if (*inp_ptr != '\n') return(c);
} while (get_text() != EOF);
return(EOF);
}
/******************************************************************/
char err_str[60];
int err_str_size = sizeof(err_str);
/******************************************************************
* Display bad string.
*/
void bad_token( char *ptr, char *msg )
/*
* At entry:
* ptr - points to character in error in inp_str
* msg - pointer to string that is the error message to display
* At exit:
* inp_str is displayed, a line terminated with '^' is displayed
* under inp_str to indicate error position and the error message
* is displayed under that.
*/
{
char *s=err_str, *is=inp_str, *os;
int lim = sizeof(err_str);
if (ptr-inp_str > lim)
{
is += (ptr-inp_str) - lim;
}
os = is;
while (is < ptr) *s++ = (*is++ != '\t') ? ' ' : '\t';
*s++ = '^';
*s = '\0';
if (strlen(os) > lim*2)
{
sprintf(emsg,"%s\n%-*.*s\n%s",msg, lim*2, lim*2, os, err_str);
}
else
{
sprintf(emsg,"%s\n%s%s",msg, os, err_str);
}
err_msg(MSG_ERROR,emsg);
}
/******************************************************************/
/******************************************************************
* Get the first char of the next token
*/
int get_token_c( void )
/*
* At entry:
* inp_ptr - points to next source char in inp_str
* inp_str - contains the source line
* At exit:
* returns EOL if end of source line
* " EOF if end of file detected
* " special token eater code if success
* token_type set to type of token detected
*/
#define TOKEN_E_nanstng 0 /* non-a/n terminated string */
#define TOKEN_E_tstrng 1 /* string terminated with token_end char */
#define TOKEN_E_idnum 2 /* decimal id number */
#define TOKEN_E_hexnum 3 /* hex number */
#define TOKEN_E_hexstng 4 /* hex string */
#define TOKEN_E_char 5 /* single char (operator, etc) */
#define TOKEN_E_decnum 6 /* decimal number */
{
unsigned char c;
while (1)
{
token_value = token_minus = 0;
if (debug > 3)
{
while (isspace(c= get_c()));
}
else
{
while (isspace(c= *inp_ptr++));
}
tkn_ptr = inp_ptr; /* point to beginning of token */
token_curchr = c; /* say what the token is */
switch (c)
{
case NULL:
case '\n': {
--inp_ptr; /* don't advance over EOL char */
token_type = EOL;
return(EOL);
}
case 255: {
token_type = EOF;
return(EOF);
}
case '.': { token_type = TOKEN_cmd; return(TOKEN_E_nanstng);}
case '{': { token_type = TOKEN_ID; token_end = '}';
return(TOKEN_E_tstrng);}
case '%': { token_type = TOKEN_ID_num; return(TOKEN_E_idnum);}
case '#': { token_type = TOKEN_const; return(TOKEN_E_hexnum);}
case '\'': { token_type = TOKEN_bins; token_end = '\'';
return(TOKEN_E_hexstng);}
case 'A':
case 'a':
case 'O':
case 'o':
case 'S':
case 's':
case '+': /* add */
case '<': /* shift left */
case '>': /* shift right */
case '*': /* multiply */
case '/': /* divide */
case '@': /* modulo */
case '|': /* logical or */
case '^': /* logical exclusive or */
case '~': /* not (1's compliment) */
case '&': /* logical and */
case '_': /* negate (2's compliment) */
case '?': /* unsigned divide */
case '!': /* relational (next char is type) */
case '$': /* dup */
case '`': /* xchg */
case '=': { /* swap bytes of a 2 byte word */
token_type = TOKEN_oper; return(TOKEN_E_char);
}
case 'u':
case 'c': { token_type = TOKEN_uc; return(TOKEN_E_char);}
case ':': { token_type = TOKEN_expr_tag; return(TOKEN_E_nanstng);}
case '\"': { token_type = TOKEN_ascs; token_end = '\"';
return(TOKEN_E_tstrng);}
case 'L':
case 'B': { token_type = TOKEN_LB; return(TOKEN_E_char);}
case '\\': {
if (*inp_ptr == '\n')
{ /* next thing a \n? */
if (get_text() == EOF) return(EOF); /* get another line */
continue; /* and keep going */
}
}
case ',': { token_type = TOKEN_sep; return(TOKEN_E_char);}
case '-': {
if (!isdigit(*inp_ptr))
{
token_type = TOKEN_oper;
return(TOKEN_E_char);
}
token_minus++;
if (debug > 3)
{
c = get_c();
}
else
{
c = *inp_ptr++;
}
token_curchr = c;
}
default: {
if (isdigit(c))
{
token_type = TOKEN_const;
return(TOKEN_E_decnum);
}
break;
}
}
bad_token(tkn_ptr,"Unrecognised token character");
token_type = EOL;
return(EOL);
}
}
/******************************************************************
* get_token - gets the next term from the input file.
*/
int get_token( int part1)
/*
* At entry:
* part1 - .lt. 0 if to do get_token_c first
* else part1 is the special token eater code
* current_fnd - pointer to file descriptor structure
* inp_ptr - points to next source byte
* inp_str - contains the source line
* At exit:
* The token is copied into the token pool and the routine
* returns a code according to the type of token processed.
* The variable token_value is set to the length of string
* type tokens or the binary value of constant type tokens.
* TOKEN_cmd - command term (.xxx)
* TOKEN_ID - identifier string ({string})
* TOKEN_ID_num - identifier number (%xxx)
* TOKEN_hexs - hex text string ('xxxx..')
* TOKEN_oper - arithimetic operator (+,-,<,>,...)
* TOKEN_expr_tag - expression tag (:c)
* TOKEN_ascs - ascii text string ("text")
* TOKEN_LB - Length/Base expression operators
* TOKEN_sep - seperator character (comma or backslash)
* EOL - newline
* EOF - end of file
*/
{
int j,c;
char *s=token_pool,*hexs;
if ((j=part1) < 0)
{
if ((j = get_token_c()) < 0 ) return(j); /* gotta be good */
}
hexs = inp_ptr;
switch (j)
{
case TOKEN_E_nanstng: { /* eat everything to non-alpha (.xxx) */
if (debug > 3)
{
while ((c= get_c()) != EOF)
{
if (isalpha(c))
{
if (++token_value < MAX_TOKEN-1) *s++ = c;
else
{
bad_token(tkn_ptr,"Token too long");
break;
}
}
else
{
if (c) --inp_ptr;
break;
}
}
}
else
{
while ((*s++ = c = *inp_ptr++) != 0)
{
if (!isalpha(c))
{
--inp_ptr;
break;
}
}
--s;
token_value = inp_ptr - hexs;
}
break;
}
case TOKEN_E_tstrng: { /* eat everything til \n or term char ("xxx") */
if (debug > 3)
{
while ((int)(c= get_c()) != EOF )
{
if (c != '\n' && c != token_end )
{
if (++token_value < MAX_TOKEN-1) *s++ = c;
else
{
bad_token(tkn_ptr,"Token too long");
break;
}
}
else break;
}
}
else
{
while (( *s++ = c = *inp_ptr++) != token_end) ;
--s;
token_value = inp_ptr - hexs - 1;
}
break;
}
case TOKEN_E_idnum: { /* get a decimal number string (%nnn) */
if (debug > 3)
{
while ((int)(c=get_c()) != EOF)
{
if (isdigit(c))
{
token_value = token_value*10 + (c-'0');
}
else
{
--inp_ptr; /* put the character back */
if (token_value > 65535)
{
bad_token(tkn_ptr,"ID value greater than 65,535");
token_value &= 65535;
}
break;
}
}
}
else
{
while ((c = *inp_ptr++) != 0)
{
if (!isdigit(c)) break;
token_value = token_value*10 + (c-'0');
}
--inp_ptr;
}
if (current_fnd->fn_max_id < token_value)
current_fnd->fn_max_id = token_value;
break;
}
case TOKEN_E_hexnum: { /* get hex number string (#xxx) */
while ((c = *inp_ptr++) != 0)
{
if (!isxdigit(c)) break;
if (!isdigit(c)) c += 9;
token_value = (token_value << 4) + (c & 0x0F);
}
--inp_ptr;
break;
}
case TOKEN_E_hexstng: { /* get hex string ('xx...') */
if (debug > 3)
{
token_value = 0;
while (1)
{
c = get_c();
if (!isxdigit(c))
{
if (token_value&1)
{
bad_token(inp_ptr,"Odd number of hex digits in string");
}
if (c != token_end)
{
bad_token(inp_ptr,"Illegal hex digit");
--inp_ptr;
}
break;
}
if (!isdigit(c)) c += 9;
if (token_value&1)
{
*s++ |= c & 0x0F;
}
else
{
*s = c << 4;
}
++token_value;
}
}
else
{
while ((c = *inp_ptr++) != token_end)
{
if (!isdigit(c)) c += 9;
*s = c << 4;
c = *inp_ptr++;
if (!isdigit(c)) c += 9;
*s++ |= c & 0x0F;
}
token_value = (inp_ptr - hexs - 1);
if (!c) --inp_ptr;
}
token_value /= 2; /* compute actual number of bytes */
break;
}
case TOKEN_E_char: { /* single character (+-,\<>) */
*s++ = token_curchr;
token_value++;
break;
}
case TOKEN_E_decnum: { /* decimal constant (nnn) */
c = token_curchr;
if (debug > 3)
{
do
{
if (isdigit(c))
{
token_value = token_value*10 + (c-'0');
}
else
{
if (c) --inp_ptr;
break;
}
} while ((int)(c= get_c()) != EOF);
}
else
{
token_value = token_value*10 + (c-'0');
while ((c = *inp_ptr++) != 0)
{
if (!isdigit(c)) break;
token_value = token_value*10 + (c-'0');
}
--inp_ptr;
}
if (token_minus) token_value = -token_value;
break;
} /* -- case */
} /* -- switch */
*s++ = '\0'; /* terminate string */
return(token_type); /* return with token type */
}
static long sym_ptrptr;
/******************************************************************
* Process ID type token. This token has the syntax of:
* [{string}]%nnn
* where the "[]" indicates an optional parameter. There may or may not
* be whitespace seperating the paramters.
*/
struct ss_struct *do_token_id( int flag )
/*
* At entry:
* flag - 0 if not to insert into symbol table.
* 1 if to insert into symbol table if symbol not found
* 2 if to insert into symbol table even if symbol found
* token_type must be set to the type of token most recently
* processed. If token_type is TOKEN_ID, then the string is made
* permanent in the token pool, the string (symbol) is added to
* the hashed symbol table and a symbol structure is picked up
* from free pool. The next token is then obtained. If token_type
* is TOKEN_ID_num, then the symbol block pointer is picked up
* from the hash table and returned to the caller. If token_type
* is neither TOKEN_ID nor TOKEN_ID_num, then an error message
* is generated indicating that an ID was expected.
* At exit:
* returns 0 if error else returns pointer to symbol block
*/
{
struct ss_struct *sym_ptr;
if (token_type == TOKEN_ID_num && !flag)
{
sym_ptrptr = id_table_base+token_value;
sym_ptr = *(id_table+sym_ptrptr);
if (sym_ptr != 0 && sym_ptr->ss_fnd == current_fnd)
{
if (sym_ptr->flg_defined)
{
bad_token(tkn_ptr,"Multiple definition of a symbol");
}
if (sym_ptr->ss_string != 0)
{
sym_delete(sym_ptr); /* remove it from symbol chain */
}
}
else
{
*(id_table+sym_ptrptr) = sym_ptr = get_symbol_block(1);
}
new_symbol = 1;
return(sym_ptr);
}
if (token_type == TOKEN_ID)
{
/*
printf("Got TOKEN_ID for symbol %s. flag=%d, token_value = %ld\n",
token_pool,flag,token_value);
*/
if (flag == 2 && token_value > 0)
{ /* if it's a segment name */
*(token_pool+token_value) = ' '; /* add a trailing space */
token_value += 1; /* to tell it from a like */
*(token_pool+token_value) = 0; /* named symbol */
}
if (flag && token_value)
{
sym_ptr = sym_lookup(token_pool,++token_value,flag);
}
else
{
sym_ptr = get_symbol_block(1);
new_symbol = 1;
}
/*
printf("\tsym_ptr=%08lX, new_symbol = %d\n",sym_ptr,new_symbol);
*/
switch (new_symbol)
{
case 5: { /* symbol added and is a duplicate (segment) */
sym_ptr->ss_string = first_symbol->ss_string;
token_value = 0; /* pretend we have no string */
} /* fall though to case 1,3 and 0 */
case 1: /* symbol is added */
case 3: { /* symbol is added, first in the hash table */
sym_ptr->ss_fnd = current_fnd;
if (token_value > 1)
{
token_pool += token_value; /* update the free mem pointer */
token_pool_size -= token_value; /* and size */
}
}
case 0: { /* no symbol added */
if (options->cross) do_xref_symbol(sym_ptr,0);
break; /* done */
}
default: {
sprintf(emsg,
"Internal error. {new_symbol} invalid (%d)\n\t%s%s%s%s",
new_symbol,"while processing {",sym_ptr->ss_string,
"} in file ",current_fnd->fn_buff);
err_msg(MSG_FATAL,emsg);
}
}
get_token(-1);
if (token_type == TOKEN_ID_num)
{
insert_id(token_value,sym_ptr); /* record the id number */
}
}
if (token_type == TOKEN_ID_num)
{
sym_ptrptr = id_table_base+token_value;
if (sym_ptrptr >= id_table_size)
{
sym_ptr = get_symbol_block(1);
insert_id(token_value,sym_ptr);
}
else
{
sym_ptr = *(id_table+sym_ptrptr);
if (sym_ptr == 0)
{
*(id_table+sym_ptrptr) = sym_ptr = get_symbol_block(1);
}
}
return(sym_ptr);
}
bad_token(tkn_ptr,"ID number expected here");
return(0);
}
/*******************************************************************
* Check that there's no more crap on the line
*/
int f1_eol( void )
/*
* At entry:
* no requirements
* At exit:
* the next line has been placed in inp_str
*/
{
get_token_c();
if (token_type != EOL)
{
bad_token(tkn_ptr,"Extraneous data at end of line, ignored");
}
return(get_text());
}
/******************************************************************
* Ignore the contents of a line
*/
int f1_featit( int flag )
/*
* At entry:
* flag - not used
* At exit:
* next line has been placed in inp_str
*/
{
return(get_text()); /* ignore the whole line, get the next */
}
/******************************************************************
* Ignore the contents of a line
*/
int f1_eatit( void )
/*
* At entry:
* At exit:
* next line has been placed in inp_str
*/
{
return(get_text()); /* ignore the whole line, get the next */
}
/*******************************************************************
* Check for multiple definition of symbol/segment
*/
int chk_mdf(int flag, SS_struct *sym_ptr, int quietly)
/*
* At entry:
* flag = 0 - testing a segment name
* 1 - testing a symbol name
* 2 - testing a group name
* sym_ptr = pointer to ss block
* quietly = 0 if to squawk about multiple defines. 1=ignore multiple defines
* expr_stack has expression of symbol to be defined.
*
* At exit:
* returns 0 if symbol multiply defined
* 1 if symbol/segment ok
*/
{
int i;
char *old_type,*new_type;
char *sy_ptr="symbol",*sg_ptr="segment",*gr_ptr="group";
i = flag * 8 +
sym_ptr->flg_group * 4 +
sym_ptr->flg_segment * 2 +
sym_ptr->flg_symbol;
switch (i)
{
default: {
sprintf(emsg,"Internal error in chk_mdf. %d %d %d %d %d {%s} %s",
i,flag,sym_ptr->flg_group,sym_ptr->flg_segment,
sym_ptr->flg_symbol,sym_ptr->ss_string,
current_fnd->fn_buff);
err_msg(MSG_FATAL,emsg);
return(0);
}
case 0:
/* Fall through to 2 */
case 2:
/* Fall through to 8 */
case 8:
/* Fall through to 16 */
case 16:
/* Fall through to 20 */
case 20:
return(1); /* it's ok */
case 9:
{
/* Testing a symbol. */
if (sym_ptr->ss_fnd == current_fnd)
return(1); /* ok if same file */
if ( expr_stack_ptr == 1 && expr_stack[0].expr_code == EXPR_VALUE )
{
/* Expression testing against is an absolute value */
if ( sym_ptr->flg_abs && sym_ptr->ss_value == expr_stack[0].expr_value )
{
/* Symbol is an absolute value */
return 1; /* values are assigned the same, so must be absolute so it's ok */
}
if ( sym_ptr->ss_exprs
&& sym_ptr->ss_exprs->len == 1
&& sym_ptr->ss_exprs->ptr->expr_code == EXPR_VALUE
&& sym_ptr->ss_exprs->ptr->expr_value == expr_stack[0].expr_value
)
{
/* Symbol is an absolute value but still attached to a single term expression */
return 1; /* values are assigned the same, so must be absolute so it's ok */
}
}
if ( !quietly )
{
sprintf(emsg,
"Multiple definition of {%s}, attempted in file %s,\n\t%s%s",
sym_ptr->ss_string,current_fnd->fn_buff,
"...previously defined in file ",sym_ptr->ss_fnd->fn_buff);
err_msg(MSG_WARN,emsg);
}
return 0;
}
case 1: { /* new segment name matches old symbol name */
new_type = sg_ptr;
old_type = sy_ptr;
break;
}
case 4: { /* new segment name matches old group name */
new_type = sg_ptr;
old_type = gr_ptr;
break;
}
case 10: { /* new symbol name matches old segment name */
new_type = sy_ptr;
old_type = sg_ptr;
break;
}
case 12: { /* new symbol name matches old group name */
new_type = sy_ptr;
old_type = gr_ptr;
break;
}
case 17: { /* new group name matches old symbol name */
new_type = gr_ptr;
old_type = sy_ptr;
break;
}
case 18: { /* new group name matches old segment name */
new_type = gr_ptr;
old_type = sg_ptr;
break;
}
}
sprintf(emsg,"{%s} defined as a %s in file %s\n\t%s%s%s%s",
sym_ptr->ss_string,new_type,current_fnd->fn_buff,
"...previously defined as a ",old_type," in file ",
sym_ptr->ss_fnd->fn_buff);
err_msg(MSG_WARN,emsg);
return(0);
}
/*******************************************************************
* Define local/global symbol
*/
int f1_defg(int flag)
/*
* At entry:
* flag - 0 if local symbol, else global symbol
* At exit:
* symbol definition written to sym_def_file
*
* Command syntax:
* .defg ID expression
* .defl ID expression
*/
{
struct ss_struct *ptr;
if ((ptr = do_token_id(flag)) == 0) return(f1_eatit());
if (options->cross)
{
if (flag != 0)
do_xref_symbol(ptr,1);
}
if (exprs(-1) < 0)
{ /* get an expression */
bad_token(tkn_ptr,"Undefined expression");
return(0);
}
if (!chk_mdf(1,ptr,0))
return(f1_eatit());
ptr->flg_symbol = 1;
ptr->flg_defined = 1; /* signal symbol found in .defg */
ptr->flg_local = !flag; /* signal symbol is local/global */
ptr->ss_fnd = current_fnd; /* record the actual file that defined it */
ptr->flg_exprs = 1; /* signal that there's an expression */
ptr->flg_nosym = current_fnd->fn_nosym;
write_to_symdef(ptr); /* write symbol stuff */
return(f1_eol());
}
/*******************************************************************
* Get length of segment
*/
int f1_len(int flag)
/*
* At entry:
* flag = not used
* At exit:
* segment length set in the segment block
*
* Command syntax:
* .len ID segment_length_constant
*/
{
struct ss_struct *sym_ptr;
struct seg_spec_struct *seg_ptr;
if (token_type == TOKEN_ID)
{
sprintf(emsg,"Segment {%s} ID declared with a .len in %s",
token_pool,current_fnd->fn_buff);
err_msg(MSG_WARN,emsg);
}
if ((sym_ptr = do_token_id(2)) == 0) return(f1_eatit());
if (!chk_mdf(0,sym_ptr,0))
{
return(f1_eatit());
}
switch (get_token(-1))
{
default: {
bad_token(tkn_ptr,"Constant expected here");
return(f1_eatit());
}
case TOKEN_const: {
if ((seg_ptr=sym_ptr->seg_spec) == 0)
{
if ((seg_ptr=get_seg_spec_mem(sym_ptr)) == 0) break;
}
seg_ptr->seg_len = token_value;
}
}
return(f1_eol());
}
/*******************************************************************
* Segment declaration
*/
int f1_seg(int flag)
/*
* At entry:
* flag = not used
* At exit:
* symbol ID inserted in symbol table(s)
*
* Command syntax:
* .seg ID constant combin identifier
* where
* constant - alignment factor
* combin - "u" for unique, "c" for common (overlaid) or
* constant for alignment of data within segment
* identifier - list of segment options {abduz} for abs,based,data,unref,zero
*/
{
int i,combin,align;
struct ss_struct *sym_ptr;
struct seg_spec_struct *seg_ptr;
#if 0
struct seg_spec *fseg_ptr;
#endif
if ((sym_ptr = do_token_id(2)) == 0) return(f1_eatit());
get_token(-1); /* get the alignment constant */
if (token_type != TOKEN_const)
{
bad_token(tkn_ptr,"Alignment constant expected here");
token_value = 0;
}
if (!chk_mdf(0,sym_ptr,0))
{
return(f1_eatit());
}
#if 0
if (new_symbol & 4) fseg_ptr = first_symbol->seg_spec;
#endif
align = token_value; /* save the alignment constant */
get_token(-1); /* get the combin argument */
if (token_type == TOKEN_uc )
{
if (token_curchr == 'c')
{