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.
156 main(int argc, char **argv)
165 fprintf(stdout, "\n");
169 for (i=1; i<argc; ++i) {
175 while ((c = *p++) != 0)
235 if (++inpfil == MAXFIL) {
236 fprintf(stderr, "too many input files\n");
239 sfp[inpfil] = afile(p, "", 0);
240 strcpy(srcfn[inpfil],afn);
246 lfp = afile(p, "lst", 1);
248 ofp = afile(p, "", 1);
250 tfp = afile(p, "sym", 1);
255 lfp = afile(p, "LST", 1);
257 ofp = afile(p, "REL", 1);
259 tfp = afile(p, "SYM", 1);
267 for (pass=0; pass<3; ++pass) {
268 if (gflag && pass == 1)
270 if (aflag && pass == 1)
272 if (oflag && pass == 2)
285 for (i = 0; i <= inpfil; i++)
303 if (setjmp(jump_env) == 0)
310 newdot(dot.s_area); /* Flush area info */
311 if (flevel || tlevel)
315 outchk(HUGE, HUGE); /* Flush */
327 /*)Function VOID asexit(i)
331 * The function asexit() explicitly closes all open
332 * files and then terminates the program.
338 * FILE * ifp[] array of include-file file handles
339 * FILE * lfp list output file handle
340 * FILE * ofp relocation output file handle
341 * FILE * tfp symbol table output file handle
342 * FILE * sfp[] array of assembler-source file handles
345 * int fclose() c-library
346 * VOID exit() c-library
349 * All files closed. Program terminates.
358 if (lfp != NULL) fclose(lfp);
359 if (ofp != NULL) fclose(ofp);
360 if (tfp != NULL) fclose(tfp);
362 for (j=0; j<MAXFIL && sfp[j] != NULL; j++) {
366 for (j=0; j<MAXINC && ifp[j] != NULL; j++) {
373 /*)Function VOID asmbl()
375 * The function asmbl() scans the assembler-source text for
376 * (1) labels, global labels, equates, global equates, and local
377 * symbols, (2) .if, .else, .endif, and .page directives,
378 * (3) machine independent assembler directives, and (4) machine
379 * dependent mnemonics.
382 * mne * mp pointer to a mne structure
383 * sym * sp pointer to a sym structure
384 * tsym * tp pointer to a tsym structure
385 * int c character from assembler-source
387 * area * ap pointer to an area structure
388 * expr e1 expression structure
389 * char id[] id string
390 * char opt[] options string
391 * char fn[] filename string
392 * char * p pointer into a string
393 * int d temporary value
394 * int n temporary value
395 * int uaf user area options flag
396 * int uf area options
399 * area * areap pointer to an area structure
400 * char ctype[] array of character types, one per
402 * int flevel IF-ELSE-ENDIF flag will be non
403 * zero for false conditional case
404 * addr_t fuzz tracks pass to pass changes in the
405 * address of symbols caused by
406 * variable length instruction formats
407 * int ifcnd[] array of IF statement condition
408 * values (0 = FALSE) indexed by tlevel
409 * int iflvl[] array of IF-ELSE-ENDIF flevel
410 * values indexed by tlevel
411 * FILE * ifp[] array of include-file file handles
412 * char incfn[][] array of include file names
413 * int incline[] current include file line
414 * int incfil current file handle index
416 * addr_t laddr address of current assembler line
417 * or value of .if argument
418 * int lmode listing mode
419 * int lop current line number on page
420 * char module[] module name string
421 * int pass assembler pass number
422 * int radix current number conversion radix:
423 * 2 (binary), 8 (octal), 10 (decimal),
425 * char stb[] Subtitle string buffer
426 * sym * symp pointer to a symbol structure
427 * char tb[] Title string buffer
428 * int tlevel current conditional level
431 * addr_t absexpr() asexpr.c
432 * area * alookup() assym.c
433 * VOID clrexpr() asexpr.c
434 * int digit() asexpr.c
435 * char endline() aslex.c
436 * VOID err() assubr.c
437 * VOID expr() asexpr.c
438 * FILE * fopen() c-library
440 * VOID getid() aslex.c
441 * int getmap() aslex.c
442 * char getnb() aslex.c
443 * VOID getst() aslex.c
444 * sym * lookup() assym.c
445 * VOID machin() ___mch.c
446 * mne * mlookup() assym.c
448 * VOID * new() assym.c
449 * VOID newdot() asmain.c
450 * VOID outall() asout.c
451 * VOID outab() asout.c
452 * VOID outchk() asout.c
453 * VOID outrb() asout.c
454 * VOID outrw() asout.c
455 * VOID phase() asmain.c
456 * VOID qerr() assubr.c
457 * char * strcpy() c-library
458 * char * strncpy() c-library
459 * VOID unget() aslex.c
465 register struct mne *mp;
466 register struct sym *sp;
467 register struct tsym *tp;
479 unsigned int mantissa, exponent;
480 const signed char readbuffer[80];
485 if ((c=endline()) == 0) { return; }
487 * If the first character is a digit then assume
488 * a local symbol is being specified. The symbol
489 * must end with $: to be valid.
491 * Construct a tsym structure at the first
492 * occurance of the symbol. Flag the symbol
493 * as multiply defined if not the first occurance.
495 * Load area, address, and fuzz values
496 * into structure tsym.
498 * Check for assembler phase error and
499 * multiply defined error.
501 if (ctype[c] & DIGIT) {
505 while ((d = digit(c, 10)) >= 0) {
509 if (c != '$' || get() != ':')
514 if (n == tp->t_num) {
521 tp=(struct tsym *) new (sizeof(struct tsym));
522 tp->t_lnk = symp->s_tsym;
525 tp->t_area = dot.s_area;
526 tp->t_addr = dot.s_addr;
531 if (n == tp->t_num) {
538 fuzz = tp->t_addr - dot.s_addr;
539 tp->t_area = dot.s_area;
540 tp->t_addr = dot.s_addr;
542 phase(tp->t_area, tp->t_addr);
543 if (tp->t_flg & S_MDF)
554 * If the first character is a letter then assume a lable,
555 * symbol, assembler directive, or assembler mnemonic is
558 if ((ctype[c] & LETTER) == 0)
567 * If the next character is a : then a label is being processed.
568 * A double :: defines a global label. If this is new label
569 * then create a symbol structure.
571 * Flag multiply defined labels.
573 * Load area, address, and fuzz values
574 * into structure symp.
576 * Check for assembler phase error and
577 * multiply defined error.
582 if ((c = get()) != ':') {
590 if ((symp->s_type != S_NEW) &&
591 ((symp->s_flag & S_ASG) == 0))
592 symp->s_flag |= S_MDF;
594 fuzz = symp->s_addr - dot.s_addr;
595 symp->s_type = S_USER;
596 symp->s_area = dot.s_area;
597 symp->s_addr = dot.s_addr;
599 if (symp->s_flag & S_MDF)
601 phase(symp->s_area, symp->s_addr);
604 symp->s_flag |= S_GBL;
610 * If the next character is a = then an equate is being processed.
611 * A double == defines a global equate. If this is new variable
612 * then create a symbol structure.
617 if ((c = get()) != '=') {
626 if (e1.e_flag || e1.e_base.e_ap != dot.s_area)
629 if (sp->s_type != S_NEW && (sp->s_flag & S_ASG) == 0) {
633 sp->s_area = e1.e_base.e_ap;
634 sp->s_addr = laddr = e1.e_addr;
643 lmode = flevel ? SLIST : CLIST;
644 if ((mp = mlookup(id)) == NULL) {
650 * If we have gotten this far then we have found an
651 * assembler directive or an assembler mnemonic.
653 * Check for .if, .else, .endif, and .page directives
654 * which are not controlled by the conditional flags
656 switch (mp->m_type) {
660 if (tlevel < MAXIF) {
663 iflvl[tlevel] = flevel;
676 if (++flevel > (iflvl[tlevel]+1)) {
680 if (--flevel < iflvl[tlevel]) {
689 flevel = iflvl[tlevel--];
707 * If we are not in a false state for .if/.else then
708 * process the assembler directives here.
710 switch (mp->m_type) {
714 laddr = dot.s_addr = (dot.s_addr + 1) & ~1;
720 laddr = dot.s_addr |= 1;
729 if (mp->m_type == S_BYTE) {
734 } while ((c = getnb()) == ',');
741 getid( readbuffer, ' ' ); /* Hack :) */
742 if ((c=getnb())=='.')
744 getid(&readbuffer[strlen(readbuffer)],'.');
749 f1 = strtod( readbuffer, (char **)NULL );
750 /* Convert f1 to a gb-lib type fp
751 * 24 bit mantissa followed by 7 bit exp and 1 bit sign
757 f2 = floor(log(fabs(f1))/log(2))+1;
758 mantissa = (0x1000000*fabs(f1))/exp(f2*log(2));
760 exponent = f2 + 0x40;
771 outab(mantissa&0xff);
772 outab((mantissa>>8)&0xff);
773 outab((mantissa>>16)&0xff);
774 outab(exponent&0xff);
776 } while ((c = getnb()) == ',');
783 if ((d = getnb()) == '\0')
785 while ((c = getmap(d)) >= 0)
787 if (mp->m_type == S_ASCIZ)
792 if ((d = getnb()) == '\0')
796 if ((n = getmap(d)) >= 0) {
808 dot.s_addr += e1.e_addr*mp->m_valu;
815 if ((c = getnb()) != 0) {
817 if (p < &tb[NTITL-1])
819 } while ((c = get()) != 0);
828 if ((c = getnb()) != 0) {
830 if (p < &stb[NSBTL-1])
832 } while ((c = get()) != 0);
845 strncpy(module, id, NCPS);
856 } while ((c = getnb()) == ',');
865 if ((c = getnb()) == '(') {
869 if (mp && mp->m_type == S_ATYP) {
875 } while ((c = getnb()) == ',');
881 if ((ap = alookup(id)) != NULL) {
882 if (uaf && uf != ap->a_flag)
885 ap = (struct area *) new (sizeof(struct area));
887 strncpy(ap->a_id, id, NCPS);
888 ap->a_ref = areap->a_ref + 1;
891 ap->a_flag = uaf ? uf : (A_CON|A_REL);
899 if (dot.s_area->a_flag & A_ABS) {
901 laddr = dot.s_addr = absexpr();
947 while ((c = get()) != d) {
948 if (p < &fn[FILSPC-1]) {
955 if (++incfil == MAXINC ||
956 (ifp[incfil] = fopen(fn, "r")) == NULL) {
962 strcpy(incfn[incfil],fn);
968 * If not an assembler directive then go to
969 * the machine dependent function which handles
970 * all the assembler mnemonics.
978 /*)Function FILE * afile(fn, ft, wf)
980 * char * fn file specification string
981 * char * ft file type string
982 * int wf read(0)/write(1) flag
984 * The function afile() opens a file for reading or writing.
985 * (1) If the file type specification string ft
986 * is not NULL then a file specification is
987 * constructed with the file path\name in fn
988 * and the extension in ft.
989 * (2) If the file type specification string ft
990 * is NULL then the file specification is
991 * constructed from fn. If fn does not have
992 * a file type then the default source file
993 * type dsft is appended to the file specification.
995 * afile() returns a file handle for the opened file or aborts
996 * the assembler on an open error.
999 * int c character value
1000 * FILE * fp filehandle for opened file
1001 * char * p1 pointer to filespec string fn
1002 * char * p2 pointer to filespec string fb
1003 * char * p3 pointer to filetype string ft
1006 * char afn[] afile() constructed filespec
1007 * char dsft[] default assembler file type string
1008 * char afn[] constructed file specification string
1011 * VOID asexit() asmain.c
1012 * FILE * fopen() c_library
1013 * int fprintf() c_library
1016 * File is opened for read or write.
1025 register char *p1, *p2, *p3;
1032 while ((c = *p1++) != 0 && c != FSEPX) {
1033 if (p2 < &afn[FILSPC-4])
1044 while ((c = *p3++) != 0) {
1045 if (p2 < &afn[FILSPC-1])
1049 if ((fp = fopen(afn, wf?"w":"r")) == NULL) {
1050 fprintf(stderr, "%s: cannot %s.\n", afn, wf?"create":"open");
1056 /*)Function VOID newdot(nap)
1058 * area * nap pointer to the new area structure
1060 * The function newdot():
1061 * (1) copies the current values of fuzz and the last
1062 * address into the current area referenced by dot
1063 * (2) loads dot with the pointer to the new area and
1064 * loads the fuzz and last address parameters
1065 * (3) outall() is called to flush any remaining
1066 * bufferred code from the old area to the output
1069 * area * oap pointer to old area
1072 * sym dot defined as sym[0]
1073 * addr_t fuzz tracks pass to pass changes in the
1074 * address of symbols caused by
1075 * variable length instruction formats
1081 * Current area saved, new area loaded, buffers flushed.
1086 register struct area *nap;
1088 register struct area *oap;
1092 oap->a_size = dot.s_addr;
1095 dot.s_addr = nap->a_size;
1099 /*)Function VOID phase(ap, a)
1101 * area * ap pointer to area
1102 * addr_t a address in area
1104 * Function phase() compares the area ap and address a
1105 * with the current area dot.s_area and address dot.s_addr
1106 * to determine if the position of the symbol has changed
1107 * between assembler passes.
1113 * sym * dot defined as sym[0]
1119 * The p error is invoked if the area and/or address
1128 if (ap != dot.s_area || a != dot.s_addr)
1134 "Usage: [-dqxgalopsf] outfile file1 [file2 file3 ...]",
1136 "Usage: [-dqxgalopsf] file1 [file2 file3 ...]",
1138 " d decimal listing",
1140 " x hex listing (default)",
1141 " g undefined symbols made global",
1142 " a all user symbols made global",
1144 " l create list output outfile[LST]",
1145 " o create object output outfile[o]",
1146 " s create symbol output outfile[SYM]",
1148 " l create list output file1[LST]",
1149 " o create object output file1[REL]",
1150 " s create symbol output file1[SYM]",
1152 " p disable listing pagination",
1153 " f flag relocatable references by ` in listing file",
1154 " ff flag relocatable references by mode in listing file",
1159 /*)Function VOID usage()
1161 * The function usage() outputs to the stderr device the
1162 * assembler name and version and a list of valid assembler options.
1165 * char ** dp pointer to an array of
1166 * text string pointers.
1169 * char cpu[] assembler type string
1170 * char * usetxt[] array of string pointers
1173 * VOID asexit() asmain.c
1174 * int fprintf() c_library
1177 * program is terminated
1185 fprintf(stderr, "\nASxxxx Assembler %s (%s)\n\n", VERSION, cpu);
1186 for (dp = usetxt; *dp; dp++)
1187 fprintf(stderr, "%s\n", *dp);