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 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
152 * Completion of main() completes the assembly process.
153 * REL, LST, and/or SYM files may be generated.
157 main(int argc, char **argv)
163 /* Check to make sure there are the right number of filenames */
164 /* before openning any of them */
170 for (i=1; i<argc; ++i) {
181 fprintf(stdout, "\n");
185 for (i=1; i<argc; ++i) {
191 while ((c = *p++) != 0)
251 if (++inpfil == MAXFIL) {
252 fprintf(stderr, "too many input files\n");
255 sfp[inpfil] = afile(p, "", 0);
256 strcpy(srcfn[inpfil],afn);
262 lfp = afile(p, "lst", 1);
264 ofp = afile(p, "", 1);
266 tfp = afile(p, "sym", 1);
271 lfp = afile(p, "LST", 1);
273 ofp = afile(p, "REL", 1);
275 tfp = afile(p, "SYM", 1);
283 for (pass=0; pass<3; ++pass) {
284 if (gflag && pass == 1)
286 if (aflag && pass == 1)
288 if (oflag && pass == 2)
301 for (i = 0; i <= inpfil; i++)
314 while (as_getline()) {
319 if (setjmp(jump_env) == 0)
326 newdot(dot.s_area); /* Flush area info */
327 if (flevel || tlevel)
331 outchk(HUGE, HUGE); /* Flush */
343 /*)Function VOID asexit(i)
347 * The function asexit() explicitly closes all open
348 * files and then terminates the program.
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
361 * int fclose() c-library
362 * VOID exit() c-library
365 * All files closed. Program terminates.
373 if (lfp != NULL) fclose(lfp);
374 if (ofp != NULL) fclose(ofp);
375 if (tfp != NULL) fclose(tfp);
377 for (j=0; j<MAXFIL && sfp[j] != NULL; j++) {
381 for (j=0; j<MAXINC && ifp[j] != NULL; j++) {
388 /*)Function VOID asmbl()
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.
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
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
414 * area * areap pointer to an area structure
415 * char ctype[] array of character types, one per
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
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),
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
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
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
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
480 register struct mne *mp;
481 register struct sym *sp;
482 register struct tsym *tp;
494 unsigned int mantissa, exponent;
495 const char readbuffer[80];
500 if ((c=endline()) == 0) { return; }
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.
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.
510 * Load area, address, and fuzz values
511 * into structure tsym.
513 * Check for assembler phase error and
514 * multiply defined error.
516 if (ctype[c] & DIGIT) {
520 while ((d = digit(c, 10)) >= 0) {
524 if (c != '$' || get() != ':')
529 if (n == tp->t_num) {
536 tp=(struct tsym *) new (sizeof(struct tsym));
537 tp->t_lnk = symp->s_tsym;
540 tp->t_area = dot.s_area;
541 tp->t_addr = dot.s_addr;
546 if (n == tp->t_num) {
553 fuzz = tp->t_addr - dot.s_addr;
554 tp->t_area = dot.s_area;
555 tp->t_addr = dot.s_addr;
557 phase(tp->t_area, tp->t_addr);
558 if (tp->t_flg & S_MDF)
569 * If the first character is a letter then assume a label,
570 * symbol, assembler directive, or assembler mnemonic is
573 if ((ctype[c] & LETTER) == 0) {
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.
587 * Flag multiply defined labels.
589 * Load area, address, and fuzz values
590 * into structure symp.
592 * Check for assembler phase error and
593 * multiply defined error.
598 if ((c = get()) != ':') {
606 if ((symp->s_type != S_NEW) &&
607 ((symp->s_flag & S_ASG) == 0))
608 symp->s_flag |= S_MDF;
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;
615 if (symp->s_flag & S_MDF)
617 phase(symp->s_area, symp->s_addr);
620 symp->s_flag |= S_GBL;
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.
633 if ((c = get()) != '=') {
642 if (e1.e_flag || e1.e_base.e_ap != dot.s_area)
645 if (sp->s_type != S_NEW && (sp->s_flag & S_ASG) == 0) {
649 sp->s_area = e1.e_base.e_ap;
650 sp->s_addr = laddr = e1.e_addr;
659 lmode = flevel ? SLIST : CLIST;
660 if ((mp = mlookup(id)) == NULL) {
666 * If we have gotten this far then we have found an
667 * assembler directive or an assembler mnemonic.
669 * Check for .if, .else, .endif, and .page directives
670 * which are not controlled by the conditional flags
672 switch (mp->m_type) {
676 if (tlevel < MAXIF) {
679 iflvl[tlevel] = flevel;
692 if (++flevel > (iflvl[tlevel]+1)) {
696 if (--flevel < iflvl[tlevel]) {
705 flevel = iflvl[tlevel--];
723 * If we are not in a false state for .if/.else then
724 * process the assembler directives here.
726 switch (mp->m_type) {
730 laddr = dot.s_addr = (dot.s_addr + 1) & ~1;
736 laddr = dot.s_addr |= 1;
745 if (mp->m_type == S_BYTE) {
750 } while ((c = getnb()) == ',');
757 getid( readbuffer, ' ' ); /* Hack :) */
758 if ((c=getnb())=='.')
760 getid(&readbuffer[strlen(readbuffer)],'.');
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
773 f2 = floor(log(fabs(f1))/log(2))+1;
774 mantissa = (unsigned int) ((0x1000000*fabs(f1))/exp(f2*log(2))) ;
776 exponent = (unsigned int) (f2 + 0x40) ;
787 outab(mantissa&0xff);
788 outab((mantissa>>8)&0xff);
789 outab((mantissa>>16)&0xff);
790 outab(exponent&0xff);
792 } while ((c = getnb()) == ',');
799 if ((d = getnb()) == '\0')
801 while ((c = getmap(d)) >= 0)
803 if (mp->m_type == S_ASCIZ)
808 if ((d = getnb()) == '\0')
812 if ((n = getmap(d)) >= 0) {
824 dot.s_addr += e1.e_addr*mp->m_valu;
831 if ((c = getnb()) != 0) {
833 if (p < &tb[NTITL-1])
835 } while ((c = get()) != 0);
844 if ((c = getnb()) != 0) {
846 if (p < &stb[NSBTL-1])
848 } while ((c = get()) != 0);
856 getst(id, getnb()); // a module can start with a digit
861 strncpy(module, id, NCPS);
869 if ((c = getnb()) != 0) {
871 if (p < &optsdcc[NINPUT-1])
873 } while ((c = get()) != 0);
885 } while ((c = getnb()) == ',');
894 if ((c = getnb()) == '(') {
898 if (mp && mp->m_type == S_ATYP) {
904 } while ((c = getnb()) == ',');
910 if ((ap = alookup(id)) != NULL) {
911 if (uaf && uf != ap->a_flag)
914 ap = (struct area *) new (sizeof(struct area));
916 ap->a_id = strsto(id);
917 ap->a_ref = areap->a_ref + 1;
921 ap->a_flag = uaf ? uf : (A_CON|A_REL);
929 if (dot.s_area->a_flag & A_ABS) {
931 laddr = dot.s_addr = absexpr();
977 while ((c = get()) != d) {
978 if (p < &fn[FILSPC-1]) {
985 if (++incfil == MAXINC ||
986 (ifp[incfil] = fopen(fn, "r")) == NULL) {
992 strcpy(incfn[incfil],fn);
998 * If not an assembler directive then go to
999 * the machine dependent function which handles
1000 * all the assembler mnemonics.
1008 /*)Function FILE * afile(fn, ft, wf)
1010 * char * fn file specification string
1011 * char * ft file type string
1012 * int wf read(0)/write(1) flag
1014 * The function afile() opens a file for reading or writing.
1015 * (1) If the file type specification string ft
1016 * is not NULL then a file specification is
1017 * constructed with the file path\name in fn
1018 * and the extension in ft.
1019 * (2) If the file type specification string ft
1020 * is NULL then the file specification is
1021 * constructed from fn. If fn does not have
1022 * a file type then the default source file
1023 * type dsft is appended to the file specification.
1025 * afile() returns a file handle for the opened file or aborts
1026 * the assembler on an open error.
1029 * int c character value
1030 * FILE * fp filehandle for opened file
1031 * char * p1 pointer to filespec string fn
1032 * char * p2 pointer to filespec string fb
1033 * char * p3 pointer to filetype string ft
1036 * char afn[] afile() constructed filespec
1037 * char dsft[] default assembler file type string
1038 * char afn[] constructed file specification string
1041 * VOID asexit() asmain.c
1042 * FILE * fopen() c_library
1043 * int fprintf() c_library
1046 * File is opened for read or write.
1050 afile(char *fn, char *ft, int wf)
1052 register char *p2, *p3;
1060 p2 = strrchr (afn, FSEPX); // search last '.'
1062 p2 = afn + strlen (afn);
1063 if (p2 > &afn[FILSPC-4]) // truncate filename, if it's too long
1064 p2 = &afn[FILSPC-4];
1067 // choose a file-extension
1068 if (*p3 == 0) { // extension supplied?
1069 p3 = strrchr (fn, FSEPX); // no: extension in fn?
1073 p3 = dsft; // no: default extension
1076 while ((c = *p3++) != 0) { // strncpy
1077 if (p2 < &afn[FILSPC-1])
1082 if ((fp = fopen(afn, wf?"w":"r")) == NULL) {
1083 fprintf(stderr, "%s: cannot %s.\n", afn, wf?"create":"open");
1089 /*)Function VOID newdot(nap)
1091 * area * nap pointer to the new area structure
1093 * The function newdot():
1094 * (1) copies the current values of fuzz and the last
1095 * address into the current area referenced by dot
1096 * (2) loads dot with the pointer to the new area and
1097 * loads the fuzz and last address parameters
1098 * (3) outall() is called to flush any remaining
1099 * bufferred code from the old area to the output
1102 * area * oap pointer to old area
1105 * sym dot defined as sym[0]
1106 * Addr_T fuzz tracks pass to pass changes in the
1107 * address of symbols caused by
1108 * variable length instruction formats
1114 * Current area saved, new area loaded, buffers flushed.
1118 newdot(struct area *nap)
1120 register struct area *oap;
1124 oap->a_size = dot.s_addr;
1127 dot.s_addr = nap->a_size;
1131 /*)Function VOID phase(ap, a)
1133 * area * ap pointer to area
1134 * Addr_T a address in area
1136 * Function phase() compares the area ap and address a
1137 * with the current area dot.s_area and address dot.s_addr
1138 * to determine if the position of the symbol has changed
1139 * between assembler passes.
1145 * sym * dot defined as sym[0]
1151 * The p error is invoked if the area and/or address
1156 phase(struct area *ap, Addr_T a)
1158 if (ap != dot.s_area || a != dot.s_addr)
1164 "Usage: [-dqxgalopsf] outfile file1 [file2 file3 ...]",
1166 "Usage: [-dqxgalopsf] file1 [file2 file3 ...]",
1168 " d decimal listing",
1170 " x hex listing (default)",
1171 " g undefined symbols made global",
1172 " a all user symbols made global",
1174 " l create list output outfile[LST]",
1175 " o create object output outfile[o]",
1176 " s create symbol output outfile[SYM]",
1178 " l create list output file1[LST]",
1179 " o create object output file1[REL]",
1180 " s create symbol output file1[SYM]",
1182 " p disable listing pagination",
1183 " f flag relocatable references by ` in listing file",
1184 " ff flag relocatable references by mode in listing file",
1189 /*)Function VOID usage()
1191 * The function usage() outputs to the stderr device the
1192 * assembler name and version and a list of valid assembler options.
1195 * char ** dp pointer to an array of
1196 * text string pointers.
1199 * char cpu[] assembler type string
1200 * char * usetxt[] array of string pointers
1203 * VOID asexit() asmain.c
1204 * int fprintf() c_library
1207 * program is terminated
1215 fprintf(stderr, "\nASxxxx Assembler %s (%s)\n\n", VERSION, cpu);
1216 for (dp = usetxt; *dp; dp++)
1217 fprintf(stderr, "%s\n", *dp);