4 * (C) Copyright 1989-1995
13 * Extensions: P. Felber
29 * The module asmain.c includes the command argument parser,
30 * the three pass sequencer, and the machine independent
31 * assembler parsing code.
33 * asmain.c contains the following functions:
34 * VOID main(argc, argv)
37 * FILE * afile(fn, ft, wf)
42 * asmain.c contains the array char *usetxt[] which
43 * references the usage text strings printed by usage().
46 /*)Function VOID main(argc, argv)
48 * int argc argument count
49 * char * argv array of pointers to argument strings
51 * The function main() is the entry point to the assembler.
52 * The purpose of main() is to (1) parse the command line
53 * arguments for options and source file specifications and
54 * (2) to process the source files through the 3 pass assembler.
55 * Before each assembler pass various variables are initialized
56 * and source files are rewound to their beginning. During each
57 * assembler pass each assembler-source text line is processed.
58 * After each assembler pass the assembler information is flushed
59 * to any opened output files and the if-else-endif processing
60 * is checked for proper termination.
62 * The function main() is also responsible for opening all
63 * output files (REL, LST, and SYM), sequencing the global (-g)
64 * and all-global (-a) variable definitions, and dumping the
65 * REL file header information.
68 * char * p pointer to argument string
69 * int c character from argument string
70 * int i argument loop counter
71 * area * ap pointer to area structure
74 * int aflag -a, make all symbols global flag
75 * char afn[] afile() constructed filespec
76 * area * areap pointer to an area structure
77 * int cb[] array of assembler output values
78 * int cbt[] array of assembler relocation types
79 * describing the data in cb[]
80 * int cfile current file handle index
81 * of input assembly files
82 * int * cp pointer to assembler output array cb[]
83 * int * cpt pointer to assembler relocation type
85 * char eb[] array of generated error codes
86 * char * ep pointer into error list array eb[]
87 * int fflag -f(f), relocations flagged flag
88 * int flevel IF-ELSE-ENDIF flag will be non
89 * zero for false conditional case
90 * addr_t fuzz tracks pass to pass changes in the
91 * address of symbols caused by
92 * variable length instruction formats
93 * int gflag -g, make undefined symbols global flag
94 * char ib[] assembler-source text line
95 * int inpfil count of assembler
96 * input files specified
97 * int ifcnd[] array of IF statement condition
98 * values (0 = FALSE) indexed by tlevel
99 * int iflvl[] array of IF-ELSE-ENDIF flevel
100 * values indexed by tlevel
101 * int incfil current file handle index
103 * char * ip pointer into the assembler-source
105 * jmp_buf jump_env compiler dependent structure
106 * used by setjmp() and longjmp()
107 * int lflag -l, generate listing flag
108 * int line current assembler source
110 * int lop current line number on page
111 * int oflag -o, generate relocatable output flag
112 * int page current page number
113 * int pflag enable listing pagination
114 * int pass assembler pass number
115 * int radix current number conversion radix:
116 * 2 (binary), 8 (octal), 10 (decimal),
118 * int sflag -s, generate symbol table flag
119 * char srcfn[][] array of source file names
120 * int srcline[] current source file line
121 * char stb[] Subtitle string buffer
122 * sym * symp pointer to a symbol structure
123 * int tlevel current conditional level
124 * int xflag -x, listing radix flag
125 * FILE * lfp list output file handle
126 * FILE * ofp relocation output file handle
127 * FILE * tfp symbol table output file handle
128 * FILE * sfp[] array of assembler-source file handles
131 * FILE * afile() asmain.c
132 * VOID allglob() assym.c
133 * VOID asexit() asmain.c
134 * VOID diag() assubr.c
135 * VOID err() assubr.c
136 * int fprintf() c-library
137 * int getline() aslex.c
138 * VOID list() aslist.c
139 * VOID lstsym() aslist.c
140 * VOID minit() ___mch.c
141 * VOID newdot() asmain.c
142 * VOID outchk() asout.c
143 * VOID outgsd() asout.c
144 * int rewind() c-library
145 * int setjmp() c-library
146 * VOID symglob() assym.c
147 * VOID syminit() assym.c
148 * VOID usage() asmain.c
151 * Completion of main() completes the assembly process.
152 * REL, LST, and/or SYM files may be generated.
166 fprintf(stdout, "\n");
170 for (i=1; i<argc; ++i) {
176 while ((c = *p++) != 0)
236 if (++inpfil == MAXFIL) {
237 fprintf(stderr, "too many input files\n");
240 sfp[inpfil] = afile(p, "", 0);
241 strcpy(srcfn[inpfil],afn);
247 lfp = afile(p, "lst", 1);
249 ofp = afile(p, "", 1);
251 tfp = afile(p, "sym", 1);
256 lfp = afile(p, "LST", 1);
258 ofp = afile(p, "REL", 1);
260 tfp = afile(p, "SYM", 1);
268 for (pass=0; pass<3; ++pass) {
269 if (gflag && pass == 1)
271 if (aflag && pass == 1)
273 if (oflag && pass == 2)
286 for (i = 0; i <= inpfil; i++)
304 if (setjmp(jump_env) == 0)
311 newdot(dot.s_area); /* Flush area info */
312 if (flevel || tlevel)
316 outchk(HUGE, HUGE); /* Flush */
326 /*)Function VOID asexit(i)
330 * The function asexit() explicitly closes all open
331 * files and then terminates the program.
337 * FILE * ifp[] array of include-file file handles
338 * FILE * lfp list output file handle
339 * FILE * ofp relocation output file handle
340 * FILE * tfp symbol table output file handle
341 * FILE * sfp[] array of assembler-source file handles
344 * int fclose() c-library
345 * VOID exit() c-library
348 * All files closed. Program terminates.
357 if (lfp != NULL) fclose(lfp);
358 if (ofp != NULL) fclose(ofp);
359 if (tfp != NULL) fclose(tfp);
361 for (j=0; j<MAXFIL && sfp[j] != NULL; j++) {
365 for (j=0; j<MAXINC && ifp[j] != NULL; j++) {
372 /*)Function VOID asmbl()
374 * The function asmbl() scans the assembler-source text for
375 * (1) labels, global labels, equates, global equates, and local
376 * symbols, (2) .if, .else, .endif, and .page directives,
377 * (3) machine independent assembler directives, and (4) machine
378 * dependent mnemonics.
381 * mne * mp pointer to a mne structure
382 * sym * sp pointer to a sym structure
383 * tsym * tp pointer to a tsym structure
384 * int c character from assembler-source
386 * area * ap pointer to an area structure
387 * expr e1 expression structure
388 * char id[] id string
389 * char opt[] options string
390 * char fn[] filename string
391 * char * p pointer into a string
392 * int d temporary value
393 * int n temporary value
394 * int uaf user area options flag
395 * int uf area options
398 * area * areap pointer to an area structure
399 * char ctype[] array of character types, one per
401 * int flevel IF-ELSE-ENDIF flag will be non
402 * zero for false conditional case
403 * addr_t fuzz tracks pass to pass changes in the
404 * address of symbols caused by
405 * variable length instruction formats
406 * int ifcnd[] array of IF statement condition
407 * values (0 = FALSE) indexed by tlevel
408 * int iflvl[] array of IF-ELSE-ENDIF flevel
409 * values indexed by tlevel
410 * FILE * ifp[] array of include-file file handles
411 * char incfn[][] array of include file names
412 * int incline[] current include file line
413 * int incfil current file handle index
415 * addr_t laddr address of current assembler line
416 * or value of .if argument
417 * int lmode listing mode
418 * int lop current line number on page
419 * char module[] module name string
420 * int pass assembler pass number
421 * int radix current number conversion radix:
422 * 2 (binary), 8 (octal), 10 (decimal),
424 * char stb[] Subtitle string buffer
425 * sym * symp pointer to a symbol structure
426 * char tb[] Title string buffer
427 * int tlevel current conditional level
430 * addr_t absexpr() asexpr.c
431 * area * alookup() assym.c
432 * VOID clrexpr() asexpr.c
433 * int digit() asexpr.c
434 * char endline() aslex.c
435 * VOID err() assubr.c
436 * VOID expr() asexpr.c
437 * FILE * fopen() c-library
439 * VOID getid() aslex.c
440 * int getmap() aslex.c
441 * char getnb() aslex.c
442 * VOID getst() aslex.c
443 * sym * lookup() assym.c
444 * VOID machin() ___mch.c
445 * mne * mlookup() assym.c
447 * VOID * new() assym.c
448 * VOID newdot() asmain.c
449 * VOID outall() asout.c
450 * VOID outab() asout.c
451 * VOID outchk() asout.c
452 * VOID outrb() asout.c
453 * VOID outrw() asout.c
454 * VOID phase() asmain.c
455 * VOID qerr() assubr.c
456 * char * strcpy() c-library
457 * char * strncpy() c-library
458 * VOID unget() aslex.c
464 register struct mne *mp;
465 register struct sym *sp;
466 register struct tsym *tp;
478 unsigned int mantissa, exponent;
479 const signed char readbuffer[80];
484 if ((c=endline()) == 0) { return; }
486 * If the first character is a digit then assume
487 * a local symbol is being specified. The symbol
488 * must end with $: to be valid.
490 * Construct a tsym structure at the first
491 * occurance of the symbol. Flag the symbol
492 * as multiply defined if not the first occurance.
494 * Load area, address, and fuzz values
495 * into structure tsym.
497 * Check for assembler phase error and
498 * multiply defined error.
500 if (ctype[c] & DIGIT) {
504 while ((d = digit(c, 10)) >= 0) {
508 if (c != '$' || get() != ':')
513 if (n == tp->t_num) {
520 tp=(struct tsym *) new (sizeof(struct tsym));
521 tp->t_lnk = symp->s_tsym;
524 tp->t_area = dot.s_area;
525 tp->t_addr = dot.s_addr;
530 if (n == tp->t_num) {
537 fuzz = tp->t_addr - dot.s_addr;
538 tp->t_area = dot.s_area;
539 tp->t_addr = dot.s_addr;
541 phase(tp->t_area, tp->t_addr);
542 if (tp->t_flg & S_MDF)
553 * If the first character is a letter then assume a lable,
554 * symbol, assembler directive, or assembler mnemonic is
557 if ((ctype[c] & LETTER) == 0)
566 * If the next character is a : then a label is being processed.
567 * A double :: defines a global label. If this is new label
568 * then create a symbol structure.
570 * Flag multiply defined labels.
572 * Load area, address, and fuzz values
573 * into structure symp.
575 * Check for assembler phase error and
576 * multiply defined error.
581 if ((c = get()) != ':') {
589 if ((symp->s_type != S_NEW) &&
590 ((symp->s_flag & S_ASG) == 0))
591 symp->s_flag |= S_MDF;
593 fuzz = symp->s_addr - dot.s_addr;
594 symp->s_type = S_USER;
595 symp->s_area = dot.s_area;
596 symp->s_addr = dot.s_addr;
598 if (symp->s_flag & S_MDF)
600 phase(symp->s_area, symp->s_addr);
603 symp->s_flag |= S_GBL;
609 * If the next character is a = then an equate is being processed.
610 * A double == defines a global equate. If this is new variable
611 * then create a symbol structure.
616 if ((c = get()) != '=') {
625 if (e1.e_flag || e1.e_base.e_ap != dot.s_area)
628 if (sp->s_type != S_NEW && (sp->s_flag & S_ASG) == 0) {
632 sp->s_area = e1.e_base.e_ap;
633 sp->s_addr = laddr = e1.e_addr;
642 lmode = flevel ? SLIST : CLIST;
643 if ((mp = mlookup(id)) == NULL) {
649 * If we have gotten this far then we have found an
650 * assembler directive or an assembler mnemonic.
652 * Check for .if, .else, .endif, and .page directives
653 * which are not controlled by the conditional flags
655 switch (mp->m_type) {
659 if (tlevel < MAXIF) {
662 iflvl[tlevel] = flevel;
675 if (++flevel > (iflvl[tlevel]+1)) {
679 if (--flevel < iflvl[tlevel]) {
688 flevel = iflvl[tlevel--];
706 * If we are not in a false state for .if/.else then
707 * process the assembler directives here.
709 switch (mp->m_type) {
713 laddr = dot.s_addr = (dot.s_addr + 1) & ~1;
719 laddr = dot.s_addr |= 1;
728 if (mp->m_type == S_BYTE) {
733 } while ((c = getnb()) == ',');
740 getid( readbuffer, ' ' ); /* Hack :) */
741 if ((c=getnb())=='.')
743 getid(&readbuffer[strlen(readbuffer)],'.');
748 f1 = strtod( readbuffer, (char **)NULL );
749 /* Convert f1 to a gb-lib type fp
750 * 24 bit mantissa followed by 7 bit exp and 1 bit sign
756 f2 = floor(log(fabs(f1))/log(2))+1;
757 mantissa = (0x1000000*fabs(f1))/exp(f2*log(2));
759 exponent = f2 + 0x40;
770 outab(mantissa&0xff);
771 outab((mantissa>>8)&0xff);
772 outab((mantissa>>16)&0xff);
773 outab(exponent&0xff);
775 } while ((c = getnb()) == ',');
782 if ((d = getnb()) == '\0')
784 while ((c = getmap(d)) >= 0)
786 if (mp->m_type == S_ASCIZ)
791 if ((d = getnb()) == '\0')
795 if ((n = getmap(d)) >= 0) {
807 dot.s_addr += e1.e_addr*mp->m_valu;
814 if ((c = getnb()) != 0) {
816 if (p < &tb[NTITL-1])
818 } while ((c = get()) != 0);
827 if ((c = getnb()) != 0) {
829 if (p < &stb[NSBTL-1])
831 } while ((c = get()) != 0);
844 strncpy(module, id, NCPS);
855 } while ((c = getnb()) == ',');
864 if ((c = getnb()) == '(') {
868 if (mp && mp->m_type == S_ATYP) {
874 } while ((c = getnb()) == ',');
880 if ((ap = alookup(id)) != NULL) {
881 if (uaf && uf != ap->a_flag)
884 ap = (struct area *) new (sizeof(struct area));
886 strncpy(ap->a_id, id, NCPS);
887 ap->a_ref = areap->a_ref + 1;
890 ap->a_flag = uaf ? uf : (A_CON|A_REL);
898 if (dot.s_area->a_flag & A_ABS) {
900 laddr = dot.s_addr = absexpr();
946 while ((c = get()) != d) {
947 if (p < &fn[FILSPC-1]) {
954 if (++incfil == MAXINC ||
955 (ifp[incfil] = fopen(fn, "r")) == NULL) {
961 strcpy(incfn[incfil],fn);
967 * If not an assembler directive then go to
968 * the machine dependent function which handles
969 * all the assembler mnemonics.
977 /*)Function FILE * afile(fn, ft, wf)
979 * char * fn file specification string
980 * char * ft file type string
981 * int wf read(0)/write(1) flag
983 * The function afile() opens a file for reading or writing.
984 * (1) If the file type specification string ft
985 * is not NULL then a file specification is
986 * constructed with the file path\name in fn
987 * and the extension in ft.
988 * (2) If the file type specification string ft
989 * is NULL then the file specification is
990 * constructed from fn. If fn does not have
991 * a file type then the default source file
992 * type dsft is appended to the file specification.
994 * afile() returns a file handle for the opened file or aborts
995 * the assembler on an open error.
998 * int c character value
999 * FILE * fp filehandle for opened file
1000 * char * p1 pointer to filespec string fn
1001 * char * p2 pointer to filespec string fb
1002 * char * p3 pointer to filetype string ft
1005 * char afn[] afile() constructed filespec
1006 * char dsft[] default assembler file type string
1007 * char afn[] constructed file specification string
1010 * VOID asexit() asmain.c
1011 * FILE * fopen() c_library
1012 * int fprintf() c_library
1015 * File is opened for read or write.
1024 register char *p1, *p2, *p3;
1031 while ((c = *p1++) != 0 && c != FSEPX) {
1032 if (p2 < &afn[FILSPC-4])
1043 while ((c = *p3++) != 0) {
1044 if (p2 < &afn[FILSPC-1])
1048 if ((fp = fopen(afn, wf?"w":"r")) == NULL) {
1049 fprintf(stderr, "%s: cannot %s.\n", afn, wf?"create":"open");
1055 /*)Function VOID newdot(nap)
1057 * area * nap pointer to the new area structure
1059 * The function newdot():
1060 * (1) copies the current values of fuzz and the last
1061 * address into the current area referenced by dot
1062 * (2) loads dot with the pointer to the new area and
1063 * loads the fuzz and last address parameters
1064 * (3) outall() is called to flush any remaining
1065 * bufferred code from the old area to the output
1068 * area * oap pointer to old area
1071 * sym dot defined as sym[0]
1072 * addr_t fuzz tracks pass to pass changes in the
1073 * address of symbols caused by
1074 * variable length instruction formats
1080 * Current area saved, new area loaded, buffers flushed.
1085 register struct area *nap;
1087 register struct area *oap;
1091 oap->a_size = dot.s_addr;
1094 dot.s_addr = nap->a_size;
1098 /*)Function VOID phase(ap, a)
1100 * area * ap pointer to area
1101 * addr_t a address in area
1103 * Function phase() compares the area ap and address a
1104 * with the current area dot.s_area and address dot.s_addr
1105 * to determine if the position of the symbol has changed
1106 * between assembler passes.
1112 * sym * dot defined as sym[0]
1118 * The p error is invoked if the area and/or address
1127 if (ap != dot.s_area || a != dot.s_addr)
1133 "Usage: [-dqxgalopsf] outfile file1 [file2 file3 ...]",
1135 "Usage: [-dqxgalopsf] file1 [file2 file3 ...]",
1137 " d decimal listing",
1139 " x hex listing (default)",
1140 " g undefined symbols made global",
1141 " a all user symbols made global",
1143 " l create list output outfile[LST]",
1144 " o create object output outfile[o]",
1145 " s create symbol output outfile[SYM]",
1147 " l create list output file1[LST]",
1148 " o create object output file1[REL]",
1149 " s create symbol output file1[SYM]",
1151 " p disable listing pagination",
1152 " f flag relocatable references by ` in listing file",
1153 " ff flag relocatable references by mode in listing file",
1158 /*)Function VOID usage()
1160 * The function usage() outputs to the stderr device the
1161 * assembler name and version and a list of valid assembler options.
1164 * char ** dp pointer to an array of
1165 * text string pointers.
1168 * char cpu[] assembler type string
1169 * char * usetxt[] array of string pointers
1172 * VOID asexit() asmain.c
1173 * int fprintf() c_library
1176 * program is terminated
1184 fprintf(stderr, "\nASxxxx Assembler %s (%s)\n\n", VERSION, cpu);
1185 for (dp = usetxt; *dp; dp++)
1186 fprintf(stderr, "%s\n", *dp);