* as/hc08/asmain.c (asexit),
[fw/sdcc] / as / hc08 / asmain.c
1 /* asmain.c */
2
3 /*
4  * (C) Copyright 1989-1995
5  * All Rights Reserved
6  *
7  * Alan R. Baldwin
8  * 721 Berkeley St.
9  * Kent, Ohio  44240
10  *
11  * 29-Oct-97 JLH pass ";!" comments to output file
12  */
13
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <setjmp.h>
17 #include <string.h>
18
19 #include "asm.h"
20 #include "strcmpi.h"
21
22 /*)Module       asmain.c
23  *
24  *      The module asmain.c includes the command argument parser,
25  *      the three pass sequencer, and the machine independent
26  *      assembler parsing code.
27  *
28  *      asmain.c contains the following functions:
29  *              VOID    main(argc, argv)
30  *              VOID    asexit()
31  *              VOID    asmbl()
32  *              FILE *  afile(fn, ft, wf)
33  *              VOID    newdot(nap)
34  *              VOID    phase(ap, a)
35  *              VOID    usage()
36  *
37  *      asmain.c contains the array char *usetxt[] which
38  *      references the usage text strings printed by usage().
39  */
40
41 /*)Function     VOID    main(argc, argv)
42  *
43  *              int     argc            argument count
44  *              char *  argv            array of pointers to argument strings
45  *
46  *      The function main() is the entry point to the assembler.
47  *      The purpose of main() is to (1) parse the command line
48  *      arguments for options and source file specifications and
49  *      (2) to process the source files through the 3 pass assembler.
50  *      Before each assembler pass various variables are initialized
51  *      and source files are rewound to their beginning.  During each
52  *      assembler pass each assembler-source text line is processed.
53  *      After each assembler pass the assembler information is flushed
54  *      to any opened output files and the if-else-endif processing
55  *      is checked for proper termination.
56  *
57  *      The function main() is also responsible for opening all
58  *      output files (REL, LST, and SYM), sequencing the global (-g)
59  *      and all-global (-a) variable definitions, and dumping the
60  *      REL file header information.
61  *
62  *      local variables:
63  *              char *  p               pointer to argument string
64  *              int     c               character from argument string
65  *              int     i               argument loop counter
66  *              area *  ap              pointer to area structure
67  *
68  *      global variables:
69  *              int     aflag           -a, make all symbols global flag
70  *              char    afn[]           afile() constructed filespec
71  *              area *  areap           pointer to an area structure
72  *              int     cb[]            array of assembler output values
73  *              int     cbt[]           array of assembler relocation types
74  *                                      describing the data in cb[]
75  *              int     cfile           current file handle index
76  *                                      of input assembly files
77  *              int *   cp              pointer to assembler output array cb[]
78  *              int *   cpt             pointer to assembler relocation type
79  *                                      output array cbt[]
80  *              char    eb[]            array of generated error codes
81  *              char *  ep              pointer into error list array eb[]
82  *              int     fflag           -f(f), relocations flagged flag
83  *              int     flevel          IF-ELSE-ENDIF flag will be non
84  *                                      zero for false conditional case
85  *              Addr_T  fuzz            tracks pass to pass changes in the
86  *                                      address of symbols caused by
87  *                                      variable length instruction formats
88  *              int     gflag           -g, make undefined symbols global flag
89  *              char    ib[]            assembler-source text line
90  *              int     inpfil          count of assembler
91  *                                      input files specified
92  *              int     ifcnd[]         array of IF statement condition
93  *                                      values (0 = FALSE) indexed by tlevel
94  *              int     iflvl[]         array of IF-ELSE-ENDIF flevel
95  *                                      values indexed by tlevel
96  *              int     incfil          current file handle index
97  *                                      for include files
98  *              char *  ip              pointer into the assembler-source
99  *                                      text line in ib[]
100  *              jmp_buf jump_env        compiler dependent structure
101  *                                      used by setjmp() and longjmp()
102  *              int     lflag           -l, generate listing flag
103  *              int     line            current assembler source
104  *                                      line number
105  *              int     lop             current line number on page
106  *              int     oflag           -o, generate relocatable output flag
107  *              int     jflag           -j, generate debug info flag
108  *              int     page            current page number
109  *              int     pflag           enable listing pagination
110  *              int     pass            assembler pass number
111  *              int     radix           current number conversion radix:
112  *                                      2 (binary), 8 (octal), 10 (decimal),
113  *                                      16 (hexadecimal)
114  *              int     sflag           -s, generate symbol table flag
115  *              char    srcfn[][]       array of source file names
116  *              int     srcline[]       current source file line
117  *              char    stb[]           Subtitle string buffer
118  *              sym *   symp            pointer to a symbol structure
119  *              int     tlevel          current conditional level
120  *              int     xflag           -x, listing radix flag
121  *              FILE *  lfp             list output file handle
122  *              FILE *  ofp             relocation output file handle
123  *              FILE *  tfp             symbol table output file handle
124  *              FILE *  sfp[]           array of assembler-source file handles
125  *
126  *      called functions:
127  *              FILE *  afile()         asmain.c
128  *              VOID    allglob()       assym.c
129  *              VOID    asexit()        asmain.c
130  *              VOID    diag()          assubr.c
131  *              VOID    err()           assubr.c
132  *              int     fprintf()       c-library
133  *              int     as_getline()    aslex.c
134  *              VOID    list()          aslist.c
135  *              VOID    lstsym()        aslist.c
136  *              VOID    minit()         ___mch.c
137  *              VOID    newdot()        asmain.c
138  *              VOID    outchk()        asout.c
139  *              VOID    outgsd()        asout.c
140  *              int     rewind()        c-library
141  *              int     setjmp()        c-library
142  *              VOID    symglob()       assym.c
143  *              VOID    syminit()       assym.c
144  *              VOID    usage()         asmain.c
145  *
146  *      side effects:
147  *              Completion of main() completes the assembly process.
148  *              REL, LST, and/or SYM files may be generated.
149  */
150
151 int fatalErrors=0;
152 char relFile[128];
153
154 int
155 main(argc, argv)
156 char *argv[];
157 {
158         register char *p;
159         register int c, i;
160         struct area *ap;
161
162         /*fprintf(stdout, "\n");*/
163         inpfil = -1;
164         pflag = 1;
165         for (i=1; i<argc; ++i) {
166                 p = argv[i];
167                 if (*p == '-') {
168                         if (inpfil >= 0)
169                                 usage();
170                         ++p;
171                         while ((c = *p++) != 0)
172                                 switch(c) {
173
174                                 case 'a':
175                                 case 'A':
176                                         ++aflag;
177                                         break;
178
179                                 case 'c':
180                                 case 'C':
181                                     ++cflag;
182                                     break;
183
184                                 case 'g':
185                                 case 'G':
186                                         ++gflag;
187                                         break;
188
189                                 case 'j':               /* JLH: debug info */
190                                 case 'J':
191                                         ++jflag;
192                                         ++oflag;        /* force object */
193                                         break;
194
195                                 case 'l':
196                                 case 'L':
197                                         ++lflag;
198                                         break;
199
200                                 case 'o':
201                                 case 'O':
202                                         ++oflag;
203                                         break;
204
205                                 case 's':
206                                 case 'S':
207                                         ++sflag;
208                                         break;
209
210                                 case 'p':
211                                 case 'P':
212                                         pflag = 0;
213                                         break;
214
215                                 case 'x':
216                                 case 'X':
217                                         xflag = 0;
218                                         break;
219
220                                 case 'q':
221                                 case 'Q':
222                                         xflag = 1;
223                                         break;
224
225                                 case 'd':
226                                 case 'D':
227                                         xflag = 2;
228                                         break;
229
230                                 case 'f':
231                                 case 'F':
232                                         ++fflag;
233                                         break;
234
235                                 default:
236                                         usage();
237                                 }
238                 } else {
239                         if (++inpfil == MAXFIL) {
240                                 fprintf(stderr, "too many input files\n");
241                                 asexit(1);
242                         }
243                         sfp[inpfil] = afile(p, "", 0);
244                         strcpy(srcfn[inpfil],afn);
245                         if (inpfil == 0) {
246                                 if (lflag)
247                                         lfp = afile(p, "lst", 1);
248                                 if (oflag) {
249                                   ofp = afile(p, "rel", 1);
250                                   // save the file name if we have to delete it on error
251                                   strcpy(relFile,afn);
252                                 }
253                                 if (sflag)
254                                         tfp = afile(p, "sym", 1);
255                         }
256                 }
257         }
258         if (inpfil < 0)
259                 usage();
260         syminit();
261         for (pass=0; pass<3; ++pass) {
262                 if (gflag && pass == 1)
263                         symglob();
264                 if (aflag && pass == 1)
265                         allglob();
266                 if (oflag && pass == 2)
267                         outgsd();
268                 flevel = 0;
269                 tlevel = 0;
270                 ifcnd[0] = 0;
271                 iflvl[0] = 0;
272                 radix = 10;
273                 srcline[0] = 0;
274                 page = 0;
275                 stb[0] = 0;
276                 lop  = NLPP;
277                 cfile = 0;
278                 incfil = -1;
279                 for (i = 0; i <= inpfil; i++)
280                         rewind(sfp[i]);
281                 ap = areap;
282                 while (ap) {
283                         ap->a_fuzz = 0;
284                         ap->a_size = 0;
285                         ap = ap->a_ap;
286                 }
287                 fuzz = 0;
288                 dot.s_addr = 0;
289                 dot.s_area = &dca;
290                 symp = &dot;
291                 minit();
292                 while (as_getline()) {
293                         cp = cb;
294                         cpt = cbt;
295                         ep = eb;
296                         ip = ib;
297
298                         /* JLH: if line begins with ";!", then
299                          * pass this comment on to the output file
300                          */
301                         if (oflag && (pass == 1) &&
302                             (ip[0] == ';') && (ip[1] == '!'))
303                         {
304                                 fprintf(ofp, "%s\n", ip );
305                         }
306                         
307                         if (setjmp(jump_env) == 0)
308                                 asmbl();
309
310                         if (pass == 2) {
311                                 diag();
312                                 list();
313                         }
314                 }
315                 newdot(dot.s_area); /* Flush area info */
316                 if (flevel || tlevel)
317                         err('i');
318         }
319         if (oflag)
320                 outchk(HUGE, HUGE);  /* Flush */
321         if (sflag) {
322                 lstsym(tfp);
323         } else
324         if (lflag) {
325                 lstsym(lfp);
326         }
327         //printf ("aserr: %d\n", aserr);
328         //printf ("fatalErrors: %d\n", fatalErrors);
329         asexit(fatalErrors);
330         return 0; // hush the compiler
331 }
332
333 /*)Function     VOID    asexit(i)
334  *
335  *                      int     i       exit code
336  *
337  *      The function asexit() explicitly closes all open
338  *      files and then terminates the program.
339  *
340  *      local variables:
341  *              int     j               loop counter
342  *
343  *      global variables:
344  *              FILE *  ifp[]           array of include-file file handles
345  *              FILE *  lfp             list output file handle
346  *              FILE *  ofp             relocation output file handle
347  *              FILE *  tfp             symbol table output file handle
348  *              FILE *  sfp[]           array of assembler-source file handles
349  *
350  *      functions called:
351  *              int     fclose()        c-library
352  *              VOID    exit()          c-library
353  *
354  *      side effects:
355  *              All files closed. Program terminates.
356  */
357
358 VOID
359 asexit(i)
360 int i;
361 {
362         int j;
363
364         if (lfp != NULL) fclose(lfp);
365         if (ofp != NULL) fclose(ofp);
366         if (tfp != NULL) fclose(tfp);
367
368         for (j=0; j<MAXFIL && sfp[j] != NULL; j++) {
369                 fclose(sfp[j]);
370         }
371
372         /*for (j=0; j<MAXINC && ifp[j] != NULL; j++) {
373                 fclose(ifp[j]);
374                 }*/
375         if (i) {
376           // remove output file
377           printf ("removing %s\n", relFile);
378           remove(relFile);
379         }
380         exit(i);
381 }
382
383 /*)Function     VOID    asmbl()
384  *
385  *      The function asmbl() scans the assembler-source text for
386  *      (1) labels, global labels, equates, global equates, and local
387  *      symbols, (2) .if, .else, .endif, and .page directives,
388  *      (3) machine independent assembler directives, and (4) machine
389  *      dependent mnemonics.
390  *
391  *      local variables:
392  *              mne *   mp              pointer to a mne structure
393  *              sym *   sp              pointer to a sym structure
394  *              tsym *  tp              pointer to a tsym structure
395  *              int     c               character from assembler-source
396  *                                      text line
397  *              area *  ap              pointer to an area structure
398  *              expr    e1              expression structure
399  *              char    id[]            id string
400  *              char    opt[]           options string
401  *              char    fn[]            filename string
402  *              char *  p               pointer into a string
403  *              int     d               temporary value
404  *              int     n               temporary value
405  *              int     uaf             user area options flag
406  *              int     uf              area options
407  *
408  *      global variables:
409  *              area *  areap           pointer to an area structure
410  *              char    ctype[]         array of character types, one per
411  *                                      ASCII character
412  *              int     flevel          IF-ELSE-ENDIF flag will be non
413  *                                      zero for false conditional case
414  *              Addr_T  fuzz            tracks pass to pass changes in the
415  *                                      address of symbols caused by
416  *                                      variable length instruction formats
417  *              int     ifcnd[]         array of IF statement condition
418  *                                      values (0 = FALSE) indexed by tlevel
419  *              int     iflvl[]         array of IF-ELSE-ENDIF flevel
420  *                                      values indexed by tlevel
421  *              FILE *  ifp[]           array of include-file file handles
422  *              char    incfn[][]       array of include file names
423  *              int     incline[]       current include file line
424  *              int     incfil          current file handle index
425  *                                      for include files
426  *              Addr_T  laddr           address of current assembler line
427  *                                      or value of .if argument
428  *              int     lmode           listing mode
429  *              int     lop             current line number on page
430  *              char    module[]        module name string
431  *              int     pass            assembler pass number
432  *              int     radix           current number conversion radix:
433  *                                      2 (binary), 8 (octal), 10 (decimal),
434  *                                      16 (hexadecimal)
435  *              char    stb[]           Subtitle string buffer
436  *              sym *   symp            pointer to a symbol structure
437  *              char    tb[]            Title string buffer
438  *              int     tlevel          current conditional level
439  *
440  *      functions called:
441  *              Addr_T  absexpr()       asexpr.c
442  *              area *  alookup()       assym.c
443  *              VOID    clrexpr()       asexpr.c
444  *              int     digit()         asexpr.c
445  *              char    endline()       aslex.c
446  *              VOID    err()           assubr.c
447  *              VOID    expr()          asexpr.c
448  *              FILE *  fopen()         c-library
449  *              char    get()           aslex.c
450  *              VOID    getid()         aslex.c
451  *              int     getmap()        aslex.c
452  *              char    getnb()         aslex.c
453  *              VOID    getst()         aslex.c
454  *              sym *   lookup()        assym.c
455  *              VOID    machine()       ___mch.c
456  *              mne *   mlookup()       assym.c
457  *              int     more()          aslex.c
458  *              VOID *  new()           assym.c
459  *              VOID    newdot()        asmain.c
460  *              VOID    outall()        asout.c
461  *              VOID    outab()         asout.c
462  *              VOID    outchk()        asout.c
463  *              VOID    outrb()         asout.c
464  *              VOID    outrw()         asout.c
465  *              VOID    phase()         asmain.c
466  *              VOID    qerr()          assubr.c
467  *              char *  strcpy()        c-library
468  *              char *  strncpy()       c-library
469  *              VOID    unget()         aslex.c
470  */
471
472 VOID
473 asmbl()
474 {
475         register struct mne *mp;
476         register struct sym *sp;
477         register struct tsym *tp;
478         register int c;
479         struct area  *ap;
480         struct expr e1;
481         char id[NCPS];
482         char opt[NCPS];
483         char fn[PATH_MAX];
484         char *p;
485         int d, n, uaf, uf;
486
487         laddr = dot.s_addr;
488         lmode = SLIST;
489 loop:
490         if ((c=endline()) == 0) { return; }
491         /*
492          * If the first character is a digit then assume
493          * a local symbol is being specified.  The symbol
494          * must end with $: to be valid.
495          *      pass 0:
496          *              Construct a tsym structure at the first
497          *              occurance of the symbol.  Flag the symbol
498          *              as multiply defined if not the first occurance.
499          *      pass 1:
500          *              Load area, address, and fuzz values
501          *              into structure tsym.
502          *      pass 2:
503          *              Check for assembler phase error and
504          *              multiply defined error.
505          */
506         if (ctype[c] & DIGIT) {
507                 if (flevel)
508                         return;
509                 n = 0;
510                 while ((d = digit(c, 10)) >= 0) {
511                         n = 10*n + d;
512                         c = get();
513                 }
514                 if (c != '$' || get() != ':')
515                         qerr();
516                 tp = symp->s_tsym;
517                 if (pass == 0) {
518                         while (tp) {
519                                 if (n == tp->t_num) {
520                                         tp->t_flg |= S_MDF;
521                                         break;
522                                 }
523                                 tp = tp->t_lnk;
524                         }
525                         if (tp == NULL) {
526                                 tp=(struct tsym *) new (sizeof(struct tsym));
527                                 tp->t_lnk = symp->s_tsym;
528                                 tp->t_num = n;
529                                 tp->t_flg = 0;
530                                 tp->t_area = dot.s_area;
531                                 tp->t_addr = dot.s_addr;
532                                 symp->s_tsym = tp;
533                         }
534                 } else {
535                         while (tp) {
536                                 if (n == tp->t_num) {
537                                         break;
538                                 }
539                                 tp = tp->t_lnk;
540                         }
541                         if (tp) {
542                                 if (pass == 1) {
543                                         fuzz = tp->t_addr - dot.s_addr;
544                                         tp->t_area = dot.s_area;
545                                         tp->t_addr = dot.s_addr;
546                                 } else {
547                                         phase(tp->t_area, tp->t_addr);
548                                         if (tp->t_flg & S_MDF)
549                                                 err('m');
550                                 }
551                         } else {
552                                 err('u');
553                         }
554                 }
555                 lmode = ALIST;
556                 goto loop;
557         }
558         /*
559          * If the first character is a letter then assume a label,
560          * symbol, assembler directive, or assembler mnemonic is
561          * being processed.
562          */
563         if ((ctype[c] & LETTER) == 0) {
564                 if (flevel) {
565                         return;
566                 } else {
567                         qerr();
568                 }
569         }
570         getid(id, c);
571         c = getnb();
572         /*
573          * If the next character is a : then a label is being processed.
574          * A double :: defines a global label.  If this is new label
575          * then create a symbol structure.
576          *      pass 0:
577          *              Flag multiply defined labels.
578          *      pass 1:
579          *              Load area, address, and fuzz values
580          *              into structure symp.
581          *      pass 2:
582          *              Check for assembler phase error and
583          *              multiply defined error.
584          */
585         if (c == ':') {
586                 if (flevel)
587                         return;
588                 if ((c = get()) != ':') {
589                         unget(c);
590                         c = 0;
591                 }
592                 symp = lookup(id);
593                 if (symp == &dot)
594                         err('.');
595                 if (pass == 0)
596                         if ((symp->s_type != S_NEW) &&
597                            ((symp->s_flag & S_ASG) == 0))
598                                 symp->s_flag |= S_MDF;
599                 if (pass != 2) {
600                         fuzz = symp->s_addr - dot.s_addr;
601                         symp->s_type = S_USER;
602                         symp->s_area = dot.s_area;
603                         symp->s_addr = dot.s_addr;
604                 } else {
605                         if (symp->s_flag & S_MDF)
606                                 err('m');
607                         phase(symp->s_area, symp->s_addr);
608                 }
609                 if (c) {
610                         symp->s_flag |= S_GBL;
611                 }
612                 lmode = ALIST;
613                 goto loop;
614         }
615         /*
616          * If the next character is a = then an equate is being processed.
617          * A double == defines a global equate.  If this is new variable
618          * then create a symbol structure.
619          */
620         if (c == '=') {
621                 if (flevel)
622                         return;
623                 if ((c = get()) != '=') {
624                         unget(c);
625                         c = 0;
626                 }
627                 clrexpr(&e1);
628                 expr(&e1, 0);
629                 sp = lookup(id);
630                 if (sp == &dot) {
631                         outall();
632                         if (e1.e_flag || e1.e_base.e_ap != dot.s_area)
633                                 err('.');
634                 } else
635                 if (sp->s_type != S_NEW && (sp->s_flag & S_ASG) == 0) {
636                         err('m');
637                 }
638                 sp->s_type = S_USER;
639                 sp->s_area = e1.e_base.e_ap;
640                 sp->s_addr = laddr = e1.e_addr;
641                 sp->s_flag |= S_ASG;
642                 if (c) {
643                         sp->s_flag |= S_GBL;
644                 }
645                 lmode = ELIST;
646                 goto loop;
647         }
648         unget(c);
649         lmode = flevel ? SLIST : CLIST;
650         if ((mp = mlookup(id)) == NULL) {
651                 if (!flevel)
652                         err('o');
653                 return;
654         }
655         /*
656          * If we have gotten this far then we have found an
657          * assembler directive or an assembler mnemonic.
658          *
659          * Check for .if, .else, .endif, and .page directives
660          * which are not controlled by the conditional flags
661          */
662         switch (mp->m_type) {
663
664         case S_IF:
665                 n = absexpr();
666                 if (tlevel < MAXIF) {
667                         ++tlevel;
668                         ifcnd[tlevel] = n;
669                         iflvl[tlevel] = flevel;
670                         if (n == 0) {
671                                 ++flevel;
672                         }
673                 } else {
674                         err('i');
675                 }
676                 lmode = ELIST;
677                 laddr = n;
678                 return;
679
680         case S_ELSE:
681                 if (ifcnd[tlevel]) {
682                         if (++flevel > (iflvl[tlevel]+1)) {
683                                 err('i');
684                         }
685                 } else {
686                         if (--flevel < iflvl[tlevel]) {
687                                 err('i');
688                         }
689                 }
690                 lmode = SLIST;
691                 return;
692
693         case S_ENDIF:
694                 if (tlevel) {
695                         flevel = iflvl[tlevel--];
696                 } else {
697                         err('i');
698                 }
699                 lmode = SLIST;
700                 return;
701
702         case S_PAGE:
703                 lop = NLPP;
704                 lmode = NLIST;
705                 return;
706
707         default:
708                 break;
709         }
710         if (flevel)
711                 return;
712         /*
713          * If we are not in a false state for .if/.else then
714          * process the assembler directives here.
715          */
716         switch (mp->m_type) {
717
718         case S_EVEN:
719                 outall();
720                 laddr = dot.s_addr = (dot.s_addr + 1) & ~1;
721                 lmode = ALIST;
722                 break;
723
724         case S_ODD:
725                 outall();
726                 laddr = dot.s_addr |= 1;
727                 lmode = ALIST;
728                 break;
729
730         case S_BYTE:
731         case S_WORD:
732                 do {
733                         clrexpr(&e1);
734                         expr(&e1, 0);
735                         if (mp->m_type == S_BYTE) {
736                                 outrb(&e1, R_NORM);
737                         } else {
738                                 outrw(&e1, R_NORM);
739                         }
740                 } while ((c = getnb()) == ',');
741                 unget(c);
742                 break;
743         
744         case S_ULEB128:
745         case S_SLEB128:
746                 do {
747                         Addr_T val = absexpr();
748                         int bit = sizeof(val)*8 - 1;
749                         int impliedBit;
750
751                         if (mp->m_type == S_ULEB128) {
752                                 impliedBit = 0;
753                         } else {
754                                 impliedBit = (val & (1 << bit)) ? 1 : 0;
755                         }
756                         while ((bit>0) && (((val & (1 << bit)) ? 1 : 0) == impliedBit)) {
757                                 bit--;
758                         }
759                         if (mp->m_type == S_SLEB128) {
760                                 bit++;
761                         }
762                         while (bit>=0) {
763                                 if (bit<7) {
764                                         outab(val & 0x7f);
765                                 } else {
766                                         outab(0x80 | (val & 0x7f));
767                                 }
768                                 bit -= 7;
769                                 val >>= 7;
770                         }
771                 } while ((c = getnb()) == ',');
772                 unget(c);
773                 break;
774
775         case S_ASCII:
776         case S_ASCIZ:
777                 if ((d = getnb()) == '\0')
778                         qerr();
779                 while ((c = getmap(d)) >= 0)
780                         outab(c);
781                 if (mp->m_type == S_ASCIZ)
782                         outab(0);
783                 break;
784
785         case S_ASCIS:
786                 if ((d = getnb()) == '\0')
787                         qerr();
788                 c = getmap(d);
789                 while (c >= 0) {
790                         if ((n = getmap(d)) >= 0) {
791                                 outab(c);
792                         } else {
793                                 outab(c | 0x80);
794                         }
795                         c = n;
796                 }
797                 break;
798
799         case S_BLK:
800                 clrexpr(&e1);
801                 expr(&e1, 0);
802                 outchk(HUGE,HUGE);
803                 dot.s_addr += e1.e_addr*mp->m_valu;
804                 lmode = BLIST;
805                 break;
806
807         case S_TITLE:
808                 p = tb;
809                 if ((c = getnb()) != 0) {
810                         do {
811                                 if (p < &tb[NTITL-1])
812                                         *p++ = c;
813                         } while ((c = get()) != 0);
814                 }
815                 *p = 0;
816                 unget(c);
817                 lmode = SLIST;
818                 break;
819
820         case S_SBTL:
821                 p = stb;
822                 if ((c = getnb()) != 0) {
823                         do {
824                                 if (p < &stb[NSBTL-1])
825                                         *p++ = c;
826                         } while ((c = get()) != 0);
827                 }
828                 *p = 0;
829                 unget(c);
830                 lmode = SLIST;
831                 break;
832
833         case S_MODUL:
834                 getst(id, getnb()); // a module can start with a digit
835                 if (pass == 0) {
836                         if (module[0]) {
837                                 err('m');
838                         } else {
839                                 strncpy(module, id, NCPS);
840                         }
841                 }
842                 lmode = SLIST;
843                 break;
844
845     case S_OPTSDCC:
846                 p = optsdcc;
847                 if ((c = getnb()) != 0) {
848                         do {
849                                 if (p < &optsdcc[NINPUT-1])
850                                         *p++ = c;
851                         } while ((c = get()) != 0);
852                 }
853                 *p = 0;
854                 unget(c);
855                 lmode = SLIST;
856         /*if (pass == 0) printf("optsdcc=%s\n", optsdcc);*/
857         break;
858
859         case S_GLOBL:
860                 do {
861                         getid(id, -1);
862                         sp = lookup(id);
863                         sp->s_flag |= S_GBL;
864                 } while ((c = getnb()) == ',');
865                 unget(c);
866                 lmode = SLIST;
867                 break;
868
869         case S_DAREA:
870                 getid(id, -1);
871                 uaf = 0;
872                 uf  = A_CON|A_REL;
873                 if ((c = getnb()) == '(') {
874                         do {
875                                 getid(opt, -1);
876                                 mp = mlookup(opt);
877                                 if (mp && mp->m_type == S_ATYP) {
878                                         ++uaf;
879                                         uf |= mp->m_valu;
880                                 } else {
881                                         err('u');
882                                 }
883                         } while ((c = getnb()) == ',');
884                         if (c != ')')
885                                 qerr();
886                 } else {
887                         unget(c);
888                 }
889                 if ((ap = alookup(id)) != NULL) {
890                         if (uaf && uf != ap->a_flag)
891                                 err('m');
892                 } else {
893                         ap = (struct area *) new (sizeof(struct area));
894                         ap->a_ap = areap;
895                         strncpy(ap->a_id, id, NCPS);
896                         ap->a_ref = areap->a_ref + 1;
897                         ap->a_size = 0;
898                         ap->a_fuzz = 0;
899                         ap->a_flag = uaf ? uf : (A_CON|A_REL);
900                         areap = ap;
901                 }
902                 newdot(ap);
903                 lmode = SLIST;
904                 break;
905
906         case S_ORG:
907                 if (dot.s_area->a_flag & A_ABS) {
908                         outall();
909                         laddr = dot.s_addr = dot.s_org = absexpr();
910                 } else {
911                         err('o');
912                 }
913                 outall();
914                 lmode = ALIST;
915                 break;
916
917         case S_RADIX:
918                 if (more()) {
919                         switch (getnb()) {
920                         case 'b':
921                         case 'B':
922                                 radix = 2;
923                                 break;
924                         case '@':
925                         case 'o':
926                         case 'O':
927                         case 'q':
928                         case 'Q':
929                                 radix = 8;
930                                 break;
931                         case 'd':
932                         case 'D':
933                                 radix = 10;
934                                 break;
935                         case 'h':
936                         case 'H':
937                         case 'x':
938                         case 'X':
939                                 radix = 16;
940                                 break;
941                         default:
942                                 radix = 10;
943                                 qerr();
944                                 break;
945                         }
946                 } else {
947                         radix = 10;
948                 }
949                 lmode = SLIST;
950                 break;
951
952         case S_INCL:
953                 d = getnb();
954                 p = fn;
955                 while ((c = get()) != d) {
956                         if (p < &fn[PATH_MAX-1]) {
957                                 *p++ = c;
958                         } else {
959                                 break;
960                         }
961                 }
962                 *p = 0;
963                 if ((++incfil == MAXINC) ||
964                     (ifp[incfil] = fopen(fn, "r")) == NULL) {
965                         --incfil;
966                         err('i');
967                 } else {
968                         lop = NLPP;
969                         incline[incfil] = 0;
970                         strcpy(incfn[incfil],fn);
971                 }
972                 lmode = SLIST;
973                 break;
974                 
975         case S_FLAT24:
976                 if (more())
977                 {
978                     getst(id, -1);
979                     
980                     if (!as_strcmpi(id, "on"))
981                     {
982                         /* Quick sanity check: size of 
983                          * Addr_T must be at least 24 bits.
984                          */
985                         if (sizeof(Addr_T) < 3)
986                         {
987                             warnBanner();
988                             fprintf(stderr,
989                                     "Cannot enable Flat24 mode: "
990                                     "host system must have 24 bit "
991                                     "or greater integers.\n");
992                         }
993                         else
994                         {
995                             flat24Mode = 1;
996                         }
997                     }
998                     else if (!as_strcmpi(id, "off"))
999                     {
1000                         flat24Mode = 0;
1001                     }
1002                     else
1003                     {
1004                         qerr();
1005                     }
1006                 }
1007                 else
1008                 {
1009                     qerr();
1010                 }
1011                 lmode = SLIST;
1012                 #if 0
1013                 printf("as8051: ds390 flat mode %sabled.\n",
1014                         flat24Mode ? "en" : "dis");
1015                 #endif
1016                 break;
1017                                                                         
1018
1019         /*
1020          * If not an assembler directive then go to
1021          * the machine dependent function which handles
1022          * all the assembler mnemonics.
1023          */
1024         default:
1025                 machine(mp);
1026                 /* if cdb information the generate the line info */
1027                 if (cflag && (pass == 1))
1028                     DefineCDB_Line();
1029
1030                 /* JLH: if -j, generate a line number symbol */
1031                 if (jflag && (pass == 1))
1032                 {
1033                    DefineNoICE_Line();
1034                 }
1035
1036         }
1037         goto loop;
1038 }
1039
1040 /*)Function     FILE *  afile(fn, ft, wf)
1041  *
1042  *              char *  fn              file specification string
1043  *              char *  ft              file type string
1044  *              int     wf              read(0)/write(1) flag
1045  *
1046  *      The function afile() opens a file for reading or writing.
1047  *              (1)     If the file type specification string ft
1048  *                      is not NULL then a file specification is
1049  *                      constructed with the file path\name in fn
1050  *                      and the extension in ft.
1051  *              (2)     If the file type specification string ft
1052  *                      is NULL then the file specification is
1053  *                      constructed from fn.  If fn does not have
1054  *                      a file type then the default source file
1055  *                      type dsft is appended to the file specification.
1056  *
1057  *      afile() returns a file handle for the opened file or aborts
1058  *      the assembler on an open error.
1059  *
1060  *      local variables:
1061  *              int     c               character value
1062  *              FILE *  fp              filehandle for opened file
1063  *              char *  p1              pointer to filespec string fn
1064  *              char *  p2              pointer to filespec string fb
1065  *              char *  p3              pointer to filetype string ft
1066  *
1067  *      global variables:
1068  *              char    afn[]           afile() constructed filespec
1069  *              char    dsft[]          default assembler file type string
1070  *              char    afn[]           constructed file specification string
1071  *
1072  *      functions called:
1073  *              VOID    asexit()        asmain.c
1074  *              FILE *  fopen()         c_library
1075  *              int     fprintf()       c_library
1076  *
1077  *      side effects:
1078  *              File is opened for read or write.
1079  */
1080
1081 FILE *
1082 afile(fn, ft, wf)
1083 char *fn;
1084 char *ft;
1085 int wf;
1086 {
1087         register char *p2, *p3;
1088         register int c;
1089         FILE *fp;
1090
1091         p2 = afn;
1092         p3 = ft;
1093
1094         strcpy (afn, fn);
1095         p2 = strrchr (afn, FSEPX);              // search last '.'
1096         if (!p2)
1097                 p2 = afn + strlen (afn);
1098         if (p2 > &afn[PATH_MAX-4])              // truncate filename, if it's too long
1099                 p2 = &afn[PATH_MAX-4];
1100         *p2++ = FSEPX;
1101
1102         // choose a file-extension
1103         if (*p3 == 0) {                                 // extension supplied?
1104                 p3 = strrchr (fn, FSEPX);       // no: extension in fn?
1105                 if (p3)
1106                         ++p3;
1107                 else
1108                         p3 = dsft;                                      // no: default extension
1109         }
1110
1111         while ((c = *p3++) != 0) {              // strncpy
1112                 if (p2 < &afn[PATH_MAX-1])
1113                         *p2++ = c;
1114         }
1115         *p2++ = 0;
1116
1117         if ((fp = fopen(afn, wf?"w":"r")) == NULL) {
1118                 fprintf(stderr, "%s: cannot %s.\n", afn, wf?"create":"open");
1119                 asexit(1);
1120         }
1121         return (fp);
1122 }
1123
1124 /*)Function     VOID    newdot(nap)
1125  *
1126  *              area *  nap             pointer to the new area structure
1127  *
1128  *      The function newdot():
1129  *              (1)     copies the current values of fuzz and the last
1130  *                      address into the current area referenced by dot
1131  *              (2)     loads dot with the pointer to the new area and
1132  *                      loads the fuzz and last address parameters
1133  *              (3)     outall() is called to flush any remaining
1134  *                      bufferred code from the old area to the output
1135  *
1136  *      local variables:
1137  *              area *  oap             pointer to old area
1138  *
1139  *      global variables:
1140  *              sym     dot             defined as sym[0]
1141  *              Addr_T  fuzz            tracks pass to pass changes in the
1142  *                                      address of symbols caused by
1143  *                                      variable length instruction formats
1144  *
1145  *      functions called:
1146  *              none
1147  *
1148  *      side effects:
1149  *              Current area saved, new area loaded, buffers flushed.
1150  */
1151
1152 VOID
1153 newdot(nap)
1154 register struct area *nap;
1155 {
1156         register struct area *oap;
1157
1158         oap = dot.s_area;
1159         /* fprintf (stderr, "%s dot.s_area->a_size: %d dot.s_addr: %d\n",
1160              oap->a_id, dot.s_area->a_size, dot.s_addr); */
1161         oap->a_fuzz = fuzz;
1162         if (oap->a_flag & A_OVR) {
1163           // the size of an overlay is the biggest size encountered
1164           if (oap->a_size < dot.s_addr) {
1165             oap->a_size = dot.s_addr;
1166           }
1167         } else if (oap->a_flag & A_ABS) {
1168           oap->a_addr = dot.s_org;
1169           oap->a_size = dot.s_addr - dot.s_org;
1170         } else {
1171           oap->a_addr = 0;
1172           oap->a_size = dot.s_addr;
1173         }
1174         if (nap->a_flag & A_OVR) {
1175           // a new overlay starts at 0, no fuzz
1176           dot.s_addr = 0;
1177           fuzz = 0;
1178         } else {
1179           dot.s_addr = nap->a_size;
1180           fuzz = nap->a_fuzz;
1181         }
1182         dot.s_area = nap;
1183         outall();
1184 }
1185
1186 /*)Function     VOID    phase(ap, a)
1187  *
1188  *              area *  ap              pointer to area
1189  *              Addr_T  a               address in area
1190  *
1191  *      Function phase() compares the area ap and address a
1192  *      with the current area dot.s_area and address dot.s_addr
1193  *      to determine if the position of the symbol has changed
1194  *      between assembler passes.
1195  *
1196  *      local variables:
1197  *              none
1198  *
1199  *      global varaibles:
1200  *              sym *   dot             defined as sym[0]
1201  *
1202  *      functions called:
1203  *              none
1204  *
1205  *      side effects:
1206  *              The p error is invoked if the area and/or address
1207  *              has changed.
1208  */
1209
1210 VOID
1211 phase(ap, a)
1212 struct area *ap;
1213 Addr_T a;
1214 {
1215         if (ap != dot.s_area || a != dot.s_addr)
1216                 err('p');
1217 }
1218
1219 char *usetxt[] = {
1220         "Usage: [-dqxjgalopsf] file1 [file2 file3 ...]",
1221         "  d    decimal listing",
1222         "  q    octal   listing",
1223         "  x    hex     listing (default)",
1224         "  j    add line number and debug information to file", /* JLH */
1225         "  g    undefined symbols made global",
1226         "  a    all user symbols made global",
1227         "  l    create list   output file1[LST]",
1228         "  o    create object output file1[REL]",
1229         "  s    create symbol output file1[SYM]",
1230         "  p    disable listing pagination",
1231         "  f    flag relocatable references by `    in listing file",
1232         " ff    flag relocatable references by mode in listing file",
1233         "",
1234         0
1235 };
1236
1237 /*)Function     VOID    usage()
1238  *
1239  *      The function usage() outputs to the stderr device the
1240  *      assembler name and version and a list of valid assembler options.
1241  *
1242  *      local variables:
1243  *              char ** dp              pointer to an array of
1244  *                                      text string pointers.
1245  *
1246  *      global variables:
1247  *              char    cpu[]           assembler type string
1248  *              char *  usetxt[]        array of string pointers
1249  *
1250  *      functions called:
1251  *              VOID    asexit()        asmain.c
1252  *              int     fprintf()       c_library
1253  *
1254  *      side effects:
1255  *              program is terminated
1256  */
1257
1258 VOID
1259 usage()
1260 {
1261         register char   **dp;
1262
1263         fprintf(stderr, "\nASxxxx Assembler %s  (%s)\n\n", VERSION, cpu);
1264         for (dp = usetxt; *dp; dp++)
1265                 fprintf(stderr, "%s\n", *dp);
1266         asexit(1);       
1267 }