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