4 * (C) Copyright 1989-1995
13 * Extensions: P. Felber
30 * The module asmain.c includes the command argument parser,
31 * the three pass sequencer, and the machine independent
32 * assembler parsing code.
34 * asmain.c contains the following functions:
35 * VOID main(argc, argv)
38 * FILE * afile(fn, ft, wf)
43 * asmain.c contains the array char *usetxt[] which
44 * references the usage text strings printed by usage().
47 /*)Function VOID main(argc, argv)
49 * int argc argument count
50 * char * argv array of pointers to argument strings
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.
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.
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
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
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
104 * char * ip pointer into the assembler-source
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
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),
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
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 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
152 * Completion of main() completes the assembly process.
153 * REL, LST, and/or SYM files may be generated.
157 main(int argc, char **argv)
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 */
328 /*)Function VOID asexit(i)
332 * The function asexit() explicitly closes all open
333 * files and then terminates the program.
339 * FILE * ifp[] array of include-file file handles
340 * FILE * lfp list output file handle
341 * FILE * ofp relocation output file handle
342 * FILE * tfp symbol table output file handle
343 * FILE * sfp[] array of assembler-source file handles
346 * int fclose() c-library
347 * VOID exit() c-library
350 * All files closed. Program terminates.
359 if (lfp != NULL) fclose(lfp);
360 if (ofp != NULL) fclose(ofp);
361 if (tfp != NULL) fclose(tfp);
363 for (j=0; j<MAXFIL && sfp[j] != NULL; j++) {
367 for (j=0; j<MAXINC && ifp[j] != NULL; j++) {
374 /*)Function VOID asmbl()
376 * The function asmbl() scans the assembler-source text for
377 * (1) labels, global labels, equates, global equates, and local
378 * symbols, (2) .if, .else, .endif, and .page directives,
379 * (3) machine independent assembler directives, and (4) machine
380 * dependent mnemonics.
383 * mne * mp pointer to a mne structure
384 * sym * sp pointer to a sym structure
385 * tsym * tp pointer to a tsym structure
386 * int c character from assembler-source
388 * area * ap pointer to an area structure
389 * expr e1 expression structure
390 * char id[] id string
391 * char opt[] options string
392 * char fn[] filename string
393 * char * p pointer into a string
394 * int d temporary value
395 * int n temporary value
396 * int uaf user area options flag
397 * int uf area options
400 * area * areap pointer to an area structure
401 * char ctype[] array of character types, one per
403 * int flevel IF-ELSE-ENDIF flag will be non
404 * zero for false conditional case
405 * Addr_T fuzz tracks pass to pass changes in the
406 * address of symbols caused by
407 * variable length instruction formats
408 * int ifcnd[] array of IF statement condition
409 * values (0 = FALSE) indexed by tlevel
410 * int iflvl[] array of IF-ELSE-ENDIF flevel
411 * values indexed by tlevel
412 * FILE * ifp[] array of include-file file handles
413 * char incfn[][] array of include file names
414 * int incline[] current include file line
415 * int incfil current file handle index
417 * Addr_T laddr address of current assembler line
418 * or value of .if argument
419 * int lmode listing mode
420 * int lop current line number on page
421 * char module[] module name string
422 * int pass assembler pass number
423 * int radix current number conversion radix:
424 * 2 (binary), 8 (octal), 10 (decimal),
426 * char stb[] Subtitle string buffer
427 * sym * symp pointer to a symbol structure
428 * char tb[] Title string buffer
429 * int tlevel current conditional level
432 * Addr_T absexpr() asexpr.c
433 * area * alookup() assym.c
434 * VOID clrexpr() asexpr.c
435 * int digit() asexpr.c
436 * char endline() aslex.c
437 * VOID err() assubr.c
438 * VOID expr() asexpr.c
439 * FILE * fopen() c-library
441 * VOID getid() aslex.c
442 * int getmap() aslex.c
443 * char getnb() aslex.c
444 * VOID getst() aslex.c
445 * sym * lookup() assym.c
446 * VOID machine() ___mch.c
447 * mne * mlookup() assym.c
449 * VOID * new() assym.c
450 * VOID newdot() asmain.c
451 * VOID outall() asout.c
452 * VOID outab() asout.c
453 * VOID outchk() asout.c
454 * VOID outrb() asout.c
455 * VOID outrw() asout.c
456 * VOID phase() asmain.c
457 * VOID qerr() assubr.c
458 * char * strcpy() c-library
459 * char * strncpy() c-library
460 * VOID unget() aslex.c
466 register struct mne *mp;
467 register struct sym *sp;
468 register struct tsym *tp;
480 unsigned int mantissa, exponent;
481 const signed char readbuffer[80];
486 if ((c=endline()) == 0) { return; }
488 * If the first character is a digit then assume
489 * a local symbol is being specified. The symbol
490 * must end with $: to be valid.
492 * Construct a tsym structure at the first
493 * occurance of the symbol. Flag the symbol
494 * as multiply defined if not the first occurance.
496 * Load area, address, and fuzz values
497 * into structure tsym.
499 * Check for assembler phase error and
500 * multiply defined error.
502 if (ctype[c] & DIGIT) {
506 while ((d = digit(c, 10)) >= 0) {
510 if (c != '$' || get() != ':')
515 if (n == tp->t_num) {
522 tp=(struct tsym *) new (sizeof(struct tsym));
523 tp->t_lnk = symp->s_tsym;
526 tp->t_area = dot.s_area;
527 tp->t_addr = dot.s_addr;
532 if (n == tp->t_num) {
539 fuzz = tp->t_addr - dot.s_addr;
540 tp->t_area = dot.s_area;
541 tp->t_addr = dot.s_addr;
543 phase(tp->t_area, tp->t_addr);
544 if (tp->t_flg & S_MDF)
555 * If the first character is a letter then assume a lable,
556 * symbol, assembler directive, or assembler mnemonic is
559 if ((ctype[c] & LETTER) == 0) {
569 * If the next character is a : then a label is being processed.
570 * A double :: defines a global label. If this is new label
571 * then create a symbol structure.
573 * Flag multiply defined labels.
575 * Load area, address, and fuzz values
576 * into structure symp.
578 * Check for assembler phase error and
579 * multiply defined error.
584 if ((c = get()) != ':') {
592 if ((symp->s_type != S_NEW) &&
593 ((symp->s_flag & S_ASG) == 0))
594 symp->s_flag |= S_MDF;
596 fuzz = symp->s_addr - dot.s_addr;
597 symp->s_type = S_USER;
598 symp->s_area = dot.s_area;
599 symp->s_addr = dot.s_addr;
601 if (symp->s_flag & S_MDF)
603 phase(symp->s_area, symp->s_addr);
606 symp->s_flag |= S_GBL;
612 * If the next character is a = then an equate is being processed.
613 * A double == defines a global equate. If this is new variable
614 * then create a symbol structure.
619 if ((c = get()) != '=') {
628 if (e1.e_flag || e1.e_base.e_ap != dot.s_area)
631 if (sp->s_type != S_NEW && (sp->s_flag & S_ASG) == 0) {
635 sp->s_area = e1.e_base.e_ap;
636 sp->s_addr = laddr = e1.e_addr;
645 lmode = flevel ? SLIST : CLIST;
646 if ((mp = mlookup(id)) == NULL) {
652 * If we have gotten this far then we have found an
653 * assembler directive or an assembler mnemonic.
655 * Check for .if, .else, .endif, and .page directives
656 * which are not controlled by the conditional flags
658 switch (mp->m_type) {
662 if (tlevel < MAXIF) {
665 iflvl[tlevel] = flevel;
678 if (++flevel > (iflvl[tlevel]+1)) {
682 if (--flevel < iflvl[tlevel]) {
691 flevel = iflvl[tlevel--];
709 * If we are not in a false state for .if/.else then
710 * process the assembler directives here.
712 switch (mp->m_type) {
716 laddr = dot.s_addr = (dot.s_addr + 1) & ~1;
722 laddr = dot.s_addr |= 1;
731 if (mp->m_type == S_BYTE) {
736 } while ((c = getnb()) == ',');
743 getid( readbuffer, ' ' ); /* Hack :) */
744 if ((c=getnb())=='.')
746 getid(&readbuffer[strlen(readbuffer)],'.');
751 f1 = strtod( readbuffer, (char **)NULL );
752 /* Convert f1 to a gb-lib type fp
753 * 24 bit mantissa followed by 7 bit exp and 1 bit sign
759 f2 = floor(log(fabs(f1))/log(2))+1;
760 mantissa = (unsigned int) ((0x1000000*fabs(f1))/exp(f2*log(2))) ;
762 exponent = (unsigned int) (f2 + 0x40) ;
773 outab(mantissa&0xff);
774 outab((mantissa>>8)&0xff);
775 outab((mantissa>>16)&0xff);
776 outab(exponent&0xff);
778 } while ((c = getnb()) == ',');
785 if ((d = getnb()) == '\0')
787 while ((c = getmap(d)) >= 0)
789 if (mp->m_type == S_ASCIZ)
794 if ((d = getnb()) == '\0')
798 if ((n = getmap(d)) >= 0) {
810 dot.s_addr += e1.e_addr*mp->m_valu;
817 if ((c = getnb()) != 0) {
819 if (p < &tb[NTITL-1])
821 } while ((c = get()) != 0);
830 if ((c = getnb()) != 0) {
832 if (p < &stb[NSBTL-1])
834 } while ((c = get()) != 0);
847 strncpy(module, id, NCPS);
858 } while ((c = getnb()) == ',');
867 if ((c = getnb()) == '(') {
871 if (mp && mp->m_type == S_ATYP) {
877 } while ((c = getnb()) == ',');
883 if ((ap = alookup(id)) != NULL) {
884 if (uaf && uf != ap->a_flag)
887 ap = (struct area *) new (sizeof(struct area));
889 strncpy(ap->a_id, id, NCPS);
890 ap->a_ref = areap->a_ref + 1;
893 ap->a_flag = uaf ? uf : (A_CON|A_REL);
901 if (dot.s_area->a_flag & A_ABS) {
903 laddr = dot.s_addr = absexpr();
949 while ((c = get()) != d) {
950 if (p < &fn[FILSPC-1]) {
957 if (++incfil == MAXINC ||
958 (ifp[incfil] = fopen(fn, "r")) == NULL) {
964 strcpy(incfn[incfil],fn);
970 * If not an assembler directive then go to
971 * the machine dependent function which handles
972 * all the assembler mnemonics.
980 /*)Function FILE * afile(fn, ft, wf)
982 * char * fn file specification string
983 * char * ft file type string
984 * int wf read(0)/write(1) flag
986 * The function afile() opens a file for reading or writing.
987 * (1) If the file type specification string ft
988 * is not NULL then a file specification is
989 * constructed with the file path\name in fn
990 * and the extension in ft.
991 * (2) If the file type specification string ft
992 * is NULL then the file specification is
993 * constructed from fn. If fn does not have
994 * a file type then the default source file
995 * type dsft is appended to the file specification.
997 * afile() returns a file handle for the opened file or aborts
998 * the assembler on an open error.
1001 * int c character value
1002 * FILE * fp filehandle for opened file
1003 * char * p1 pointer to filespec string fn
1004 * char * p2 pointer to filespec string fb
1005 * char * p3 pointer to filetype string ft
1008 * char afn[] afile() constructed filespec
1009 * char dsft[] default assembler file type string
1010 * char afn[] constructed file specification string
1013 * VOID asexit() asmain.c
1014 * FILE * fopen() c_library
1015 * int fprintf() c_library
1018 * File is opened for read or write.
1027 register char *p2, *p3;
1035 p2 = strrchr (afn, FSEPX); // search last '.'
1037 p2 = afn + strlen (afn);
1038 if (p2 > &afn[FILSPC-4]) // truncate filename, if it's too long
1039 p2 = &afn[FILSPC-4];
1042 // choose a file-extension
1043 if (*p3 == 0) { // extension supplied?
1044 p3 = strrchr (fn, FSEPX); // no: extension in fn?
1048 p3 = dsft; // no: default extension
1051 while ((c = *p3++) != 0) { // strncpy
1052 if (p2 < &afn[FILSPC-1])
1057 if ((fp = fopen(afn, wf?"w":"r")) == NULL) {
1058 fprintf(stderr, "%s: cannot %s.\n", afn, wf?"create":"open");
1064 /*)Function VOID newdot(nap)
1066 * area * nap pointer to the new area structure
1068 * The function newdot():
1069 * (1) copies the current values of fuzz and the last
1070 * address into the current area referenced by dot
1071 * (2) loads dot with the pointer to the new area and
1072 * loads the fuzz and last address parameters
1073 * (3) outall() is called to flush any remaining
1074 * bufferred code from the old area to the output
1077 * area * oap pointer to old area
1080 * sym dot defined as sym[0]
1081 * Addr_T fuzz tracks pass to pass changes in the
1082 * address of symbols caused by
1083 * variable length instruction formats
1089 * Current area saved, new area loaded, buffers flushed.
1094 register struct area *nap;
1096 register struct area *oap;
1100 oap->a_size = dot.s_addr;
1103 dot.s_addr = nap->a_size;
1107 /*)Function VOID phase(ap, a)
1109 * area * ap pointer to area
1110 * Addr_T a address in area
1112 * Function phase() compares the area ap and address a
1113 * with the current area dot.s_area and address dot.s_addr
1114 * to determine if the position of the symbol has changed
1115 * between assembler passes.
1121 * sym * dot defined as sym[0]
1127 * The p error is invoked if the area and/or address
1136 if (ap != dot.s_area || a != dot.s_addr)
1142 "Usage: [-dqxgalopsf] outfile file1 [file2 file3 ...]",
1144 "Usage: [-dqxgalopsf] file1 [file2 file3 ...]",
1146 " d decimal listing",
1148 " x hex listing (default)",
1149 " g undefined symbols made global",
1150 " a all user symbols made global",
1152 " l create list output outfile[LST]",
1153 " o create object output outfile[o]",
1154 " s create symbol output outfile[SYM]",
1156 " l create list output file1[LST]",
1157 " o create object output file1[REL]",
1158 " s create symbol output file1[SYM]",
1160 " p disable listing pagination",
1161 " f flag relocatable references by ` in listing file",
1162 " ff flag relocatable references by mode in listing file",
1167 /*)Function VOID usage()
1169 * The function usage() outputs to the stderr device the
1170 * assembler name and version and a list of valid assembler options.
1173 * char ** dp pointer to an array of
1174 * text string pointers.
1177 * char cpu[] assembler type string
1178 * char * usetxt[] array of string pointers
1181 * VOID asexit() asmain.c
1182 * int fprintf() c_library
1185 * program is terminated
1193 fprintf(stderr, "\nASxxxx Assembler %s (%s)\n\n", VERSION, cpu);
1194 for (dp = usetxt; *dp; dp++)
1195 fprintf(stderr, "%s\n", *dp);