4 * (C) Copyright 1989-1995
13 * Extensions: P. Felber
14 * 13-Feb-08 AD -j and -c as in 8051 as
31 * The module asmain.c includes the command argument parser,
32 * the three pass sequencer, and the machine independent
33 * assembler parsing code.
35 * asmain.c contains the following functions:
36 * VOID main(argc, argv)
39 * FILE * afile(fn, ft, wf)
44 * asmain.c contains the array char *usetxt[] which
45 * references the usage text strings printed by usage().
48 /*)Function VOID main(argc, argv)
50 * int argc argument count
51 * char * argv array of pointers to argument strings
53 * The function main() is the entry point to the assembler.
54 * The purpose of main() is to (1) parse the command line
55 * arguments for options and source file specifications and
56 * (2) to process the source files through the 3 pass assembler.
57 * Before each assembler pass various variables are initialized
58 * and source files are rewound to their beginning. During each
59 * assembler pass each assembler-source text line is processed.
60 * After each assembler pass the assembler information is flushed
61 * to any opened output files and the if-else-endif processing
62 * is checked for proper termination.
64 * The function main() is also responsible for opening all
65 * output files (REL, LST, and SYM), sequencing the global (-g)
66 * and all-global (-a) variable definitions, and dumping the
67 * REL file header information.
70 * char * p pointer to argument string
71 * int c character from argument string
72 * int i argument loop counter
73 * area * ap pointer to area structure
76 * int aflag -a, make all symbols global flag
77 * char afn[] afile() constructed filespec
78 * area * areap pointer to an area structure
79 * int cb[] array of assembler output values
80 * int cbt[] array of assembler relocation types
81 * describing the data in cb[]
82 * int cfile current file handle index
83 * of input assembly files
84 * int * cp pointer to assembler output array cb[]
85 * int * cpt pointer to assembler relocation type
87 * char eb[] array of generated error codes
88 * char * ep pointer into error list array eb[]
89 * int fflag -f(f), relocations flagged flag
90 * int flevel IF-ELSE-ENDIF flag will be non
91 * zero for false conditional case
92 * Addr_T fuzz tracks pass to pass changes in the
93 * address of symbols caused by
94 * variable length instruction formats
95 * int gflag -g, make undefined symbols global flag
96 * char ib[] assembler-source text line
97 * int inpfil count of assembler
98 * input files specified
99 * int ifcnd[] array of IF statement condition
100 * values (0 = FALSE) indexed by tlevel
101 * int iflvl[] array of IF-ELSE-ENDIF flevel
102 * values indexed by tlevel
103 * int incfil current file handle index
105 * char * ip pointer into the assembler-source
107 * jmp_buf jump_env compiler dependent structure
108 * used by setjmp() and longjmp()
109 * int lflag -l, generate listing flag
110 * int line current assembler source
112 * int lop current line number on page
113 * int oflag -o, generate relocatable output flag
114 * int jflag -j, generate debug info flag
115 * int page current page number
116 * int pflag enable listing pagination
117 * int pass assembler pass number
118 * int radix current number conversion radix:
119 * 2 (binary), 8 (octal), 10 (decimal),
121 * int sflag -s, generate symbol table flag
122 * char srcfn[][] array of source file names
123 * int srcline[] current source file line
124 * char stb[] Subtitle string buffer
125 * sym * symp pointer to a symbol structure
126 * int tlevel current conditional level
127 * int xflag -x, listing radix flag
128 * FILE * lfp list output file handle
129 * FILE * ofp relocation output file handle
130 * FILE * tfp symbol table output file handle
131 * FILE * sfp[] array of assembler-source file handles
134 * FILE * afile() asmain.c
135 * VOID allglob() assym.c
136 * VOID asexit() asmain.c
137 * VOID diag() assubr.c
138 * VOID err() assubr.c
139 * int fprintf() c-library
140 * int as_getline() aslex.c
141 * VOID list() aslist.c
142 * VOID lstsym() aslist.c
143 * VOID minit() ___mch.c
144 * VOID newdot() asmain.c
145 * VOID outchk() asout.c
146 * VOID outgsd() asout.c
147 * int rewind() c-library
148 * int setjmp() c-library
149 * VOID symglob() assym.c
150 * VOID syminit() assym.c
151 * VOID usage() asmain.c
154 * Completion of main() completes the assembly process.
155 * REL, LST, and/or SYM files may be generated.
159 main(int argc, char **argv)
165 /* Check to make sure there are the right number of filenames */
166 /* before openning any of them */
172 for (i=1; i<argc; ++i) {
183 fprintf(stdout, "\n");
187 for (i=1; i<argc; ++i) {
193 while ((c = *p++) != 0)
211 case 'j': /* JLH: debug info */
214 ++oflag; /* force object */
264 if (++inpfil == MAXFIL) {
265 fprintf(stderr, "too many input files\n");
268 sfp[inpfil] = afile(p, "", 0);
269 strcpy(srcfn[inpfil],afn);
275 lfp = afile(p, "lst", 1);
277 ofp = afile(p, "", 1);
279 tfp = afile(p, "sym", 1);
284 lfp = afile(p, "LST", 1);
286 ofp = afile(p, "REL", 1);
288 tfp = afile(p, "SYM", 1);
296 for (pass=0; pass<3; ++pass) {
297 if (gflag && pass == 1)
299 if (aflag && pass == 1)
301 if (oflag && pass == 2)
314 for (i = 0; i <= inpfil; i++)
327 while (as_getline()) {
332 if (setjmp(jump_env) == 0)
339 newdot(dot.s_area); /* Flush area info */
340 if (flevel || tlevel)
344 outchk(HUGE, HUGE); /* Flush */
356 /*)Function VOID asexit(i)
360 * The function asexit() explicitly closes all open
361 * files and then terminates the program.
367 * FILE * ifp[] array of include-file file handles
368 * FILE * lfp list output file handle
369 * FILE * ofp relocation output file handle
370 * FILE * tfp symbol table output file handle
371 * FILE * sfp[] array of assembler-source file handles
374 * int fclose() c-library
375 * VOID exit() c-library
378 * All files closed. Program terminates.
386 if (lfp != NULL) fclose(lfp);
387 if (ofp != NULL) fclose(ofp);
388 if (tfp != NULL) fclose(tfp);
390 for (j=0; j<MAXFIL && sfp[j] != NULL; j++) {
394 for (j=0; j<MAXINC && ifp[j] != NULL; j++) {
401 /*)Function VOID asmbl()
403 * The function asmbl() scans the assembler-source text for
404 * (1) labels, global labels, equates, global equates, and local
405 * symbols, (2) .if, .else, .endif, and .page directives,
406 * (3) machine independent assembler directives, and (4) machine
407 * dependent mnemonics.
410 * mne * mp pointer to a mne structure
411 * sym * sp pointer to a sym structure
412 * tsym * tp pointer to a tsym structure
413 * int c character from assembler-source
415 * area * ap pointer to an area structure
416 * expr e1 expression structure
417 * char id[] id string
418 * char opt[] options string
419 * char fn[] filename string
420 * char * p pointer into a string
421 * int d temporary value
422 * int n temporary value
423 * int uaf user area options flag
424 * int uf area options
427 * area * areap pointer to an area structure
428 * char ctype[] array of character types, one per
430 * int flevel IF-ELSE-ENDIF flag will be non
431 * zero for false conditional case
432 * Addr_T fuzz tracks pass to pass changes in the
433 * address of symbols caused by
434 * variable length instruction formats
435 * int ifcnd[] array of IF statement condition
436 * values (0 = FALSE) indexed by tlevel
437 * int iflvl[] array of IF-ELSE-ENDIF flevel
438 * values indexed by tlevel
439 * FILE * ifp[] array of include-file file handles
440 * char incfn[][] array of include file names
441 * int incline[] current include file line
442 * int incfil current file handle index
444 * Addr_T laddr address of current assembler line
445 * or value of .if argument
446 * int lmode listing mode
447 * int lop current line number on page
448 * char module[] module name string
449 * int pass assembler pass number
450 * int radix current number conversion radix:
451 * 2 (binary), 8 (octal), 10 (decimal),
453 * char stb[] Subtitle string buffer
454 * sym * symp pointer to a symbol structure
455 * char tb[] Title string buffer
456 * int tlevel current conditional level
459 * Addr_T absexpr() asexpr.c
460 * area * alookup() assym.c
461 * VOID clrexpr() asexpr.c
462 * int digit() asexpr.c
463 * char endline() aslex.c
464 * VOID err() assubr.c
465 * VOID expr() asexpr.c
466 * FILE * fopen() c-library
468 * VOID getid() aslex.c
469 * int getmap() aslex.c
470 * char getnb() aslex.c
471 * VOID getst() aslex.c
472 * sym * lookup() assym.c
473 * VOID machine() ___mch.c
474 * mne * mlookup() assym.c
476 * VOID * new() assym.c
477 * VOID newdot() asmain.c
478 * VOID outall() asout.c
479 * VOID outab() asout.c
480 * VOID outchk() asout.c
481 * VOID outrb() asout.c
482 * VOID outrw() asout.c
483 * VOID phase() asmain.c
484 * VOID qerr() assubr.c
485 * char * strcpy() c-library
486 * char * strncpy() c-library
487 * VOID unget() aslex.c
493 register struct mne *mp;
494 register struct sym *sp;
495 register struct tsym *tp;
507 unsigned int mantissa, exponent;
508 const char readbuffer[80];
513 if ((c=endline()) == 0) { return; }
515 * If the first character is a digit then assume
516 * a local symbol is being specified. The symbol
517 * must end with $: to be valid.
519 * Construct a tsym structure at the first
520 * occurance of the symbol. Flag the symbol
521 * as multiply defined if not the first occurance.
523 * Load area, address, and fuzz values
524 * into structure tsym.
526 * Check for assembler phase error and
527 * multiply defined error.
529 if (ctype[c] & DIGIT) {
533 while ((d = digit(c, 10)) >= 0) {
537 if (c != '$' || get() != ':')
542 if (n == tp->t_num) {
549 tp=(struct tsym *) new (sizeof(struct tsym));
550 tp->t_lnk = symp->s_tsym;
553 tp->t_area = dot.s_area;
554 tp->t_addr = dot.s_addr;
559 if (n == tp->t_num) {
566 fuzz = tp->t_addr - dot.s_addr;
567 tp->t_area = dot.s_area;
568 tp->t_addr = dot.s_addr;
570 phase(tp->t_area, tp->t_addr);
571 if (tp->t_flg & S_MDF)
582 * If the first character is a letter then assume a label,
583 * symbol, assembler directive, or assembler mnemonic is
586 if ((ctype[c] & LETTER) == 0) {
596 * If the next character is a : then a label is being processed.
597 * A double :: defines a global label. If this is new label
598 * then create a symbol structure.
600 * Flag multiply defined labels.
602 * Load area, address, and fuzz values
603 * into structure symp.
605 * Check for assembler phase error and
606 * multiply defined error.
611 if ((c = get()) != ':') {
619 if ((symp->s_type != S_NEW) &&
620 ((symp->s_flag & S_ASG) == 0))
621 symp->s_flag |= S_MDF;
623 fuzz = symp->s_addr - dot.s_addr;
624 symp->s_type = S_USER;
625 symp->s_area = dot.s_area;
626 symp->s_addr = dot.s_addr;
628 if (symp->s_flag & S_MDF)
630 phase(symp->s_area, symp->s_addr);
633 symp->s_flag |= S_GBL;
639 * If the next character is a = then an equate is being processed.
640 * A double == defines a global equate. If this is new variable
641 * then create a symbol structure.
646 if ((c = get()) != '=') {
655 if (e1.e_flag || e1.e_base.e_ap != dot.s_area)
658 if (sp->s_type != S_NEW && (sp->s_flag & S_ASG) == 0) {
662 sp->s_area = e1.e_base.e_ap;
663 sp->s_addr = laddr = e1.e_addr;
672 lmode = flevel ? SLIST : CLIST;
673 if ((mp = mlookup(id)) == NULL) {
679 * If we have gotten this far then we have found an
680 * assembler directive or an assembler mnemonic.
682 * Check for .if, .else, .endif, and .page directives
683 * which are not controlled by the conditional flags
685 switch (mp->m_type) {
689 if (tlevel < MAXIF) {
692 iflvl[tlevel] = flevel;
705 if (++flevel > (iflvl[tlevel]+1)) {
709 if (--flevel < iflvl[tlevel]) {
718 flevel = iflvl[tlevel--];
736 * If we are not in a false state for .if/.else then
737 * process the assembler directives here.
739 switch (mp->m_type) {
743 laddr = dot.s_addr = (dot.s_addr + 1) & ~1;
749 laddr = dot.s_addr |= 1;
758 if (mp->m_type == S_BYTE) {
763 } while ((c = getnb()) == ',');
770 getid( readbuffer, ' ' ); /* Hack :) */
771 if ((c=getnb())=='.')
773 getid(&readbuffer[strlen(readbuffer)],'.');
778 f1 = strtod( readbuffer, (char **)NULL );
779 /* Convert f1 to a gb-lib type fp
780 * 24 bit mantissa followed by 7 bit exp and 1 bit sign
786 f2 = floor(log(fabs(f1))/log(2))+1;
787 mantissa = (unsigned int) ((0x1000000*fabs(f1))/exp(f2*log(2))) ;
789 exponent = (unsigned int) (f2 + 0x40) ;
800 outab(mantissa&0xff);
801 outab((mantissa>>8)&0xff);
802 outab((mantissa>>16)&0xff);
803 outab(exponent&0xff);
805 } while ((c = getnb()) == ',');
812 if ((d = getnb()) == '\0')
814 while ((c = getmap(d)) >= 0)
816 if (mp->m_type == S_ASCIZ)
821 if ((d = getnb()) == '\0')
825 if ((n = getmap(d)) >= 0) {
837 dot.s_addr += e1.e_addr*mp->m_valu;
844 if ((c = getnb()) != 0) {
846 if (p < &tb[NTITL-1])
848 } while ((c = get()) != 0);
857 if ((c = getnb()) != 0) {
859 if (p < &stb[NSBTL-1])
861 } while ((c = get()) != 0);
869 getst(id, getnb()); // a module can start with a digit
874 strncpy(module, id, NCPS);
882 if ((c = getnb()) != 0) {
884 if (p < &optsdcc[NINPUT-1])
886 } while ((c = get()) != 0);
898 } while ((c = getnb()) == ',');
907 if ((c = getnb()) == '(') {
911 if (mp && mp->m_type == S_ATYP) {
917 } while ((c = getnb()) == ',');
923 if ((ap = alookup(id)) != NULL) {
924 if (uaf && uf != ap->a_flag)
927 ap = (struct area *) new (sizeof(struct area));
929 ap->a_id = strsto(id);
930 ap->a_ref = areap->a_ref + 1;
933 ap->a_flag = uaf ? uf : (A_CON|A_REL);
941 if (dot.s_area->a_flag & A_ABS) {
943 laddr = dot.s_addr = absexpr();
989 while ((c = get()) != d) {
990 if (p < &fn[FILSPC-1]) {
997 if (++incfil == MAXINC ||
998 (ifp[incfil] = fopen(fn, "r")) == NULL) {
1003 incline[incfil] = 0;
1004 strcpy(incfn[incfil],fn);
1010 * If not an assembler directive then go to
1011 * the machine dependent function which handles
1012 * all the assembler mnemonics.
1015 /* if cdb information then generate the line info */
1016 if (cflag && (pass == 1))
1019 /* JLH: if -j, generate a line number symbol */
1020 if (jflag && (pass == 1))
1031 /*)Function FILE * afile(fn, ft, wf)
1033 * char * fn file specification string
1034 * char * ft file type string
1035 * int wf read(0)/write(1) flag
1037 * The function afile() opens a file for reading or writing.
1038 * (1) If the file type specification string ft
1039 * is not NULL then a file specification is
1040 * constructed with the file path\name in fn
1041 * and the extension in ft.
1042 * (2) If the file type specification string ft
1043 * is NULL then the file specification is
1044 * constructed from fn. If fn does not have
1045 * a file type then the default source file
1046 * type dsft is appended to the file specification.
1048 * afile() returns a file handle for the opened file or aborts
1049 * the assembler on an open error.
1052 * int c character value
1053 * FILE * fp filehandle for opened file
1054 * char * p1 pointer to filespec string fn
1055 * char * p2 pointer to filespec string fb
1056 * char * p3 pointer to filetype string ft
1059 * char afn[] afile() constructed filespec
1060 * char dsft[] default assembler file type string
1061 * char afn[] constructed file specification string
1064 * VOID asexit() asmain.c
1065 * FILE * fopen() c_library
1066 * int fprintf() c_library
1069 * File is opened for read or write.
1073 afile(char *fn, char *ft, int wf)
1075 register char *p2, *p3;
1083 p2 = strrchr (afn, FSEPX); // search last '.'
1085 p2 = afn + strlen (afn);
1086 if (p2 > &afn[FILSPC-4]) // truncate filename, if it's too long
1087 p2 = &afn[FILSPC-4];
1090 // choose a file-extension
1091 if (*p3 == 0) { // extension supplied?
1092 p3 = strrchr (fn, FSEPX); // no: extension in fn?
1096 p3 = dsft; // no: default extension
1099 while ((c = *p3++) != 0) { // strncpy
1100 if (p2 < &afn[FILSPC-1])
1105 if ((fp = fopen(afn, wf?"w":"r")) == NULL) {
1106 fprintf(stderr, "%s: cannot %s.\n", afn, wf?"create":"open");
1112 /*)Function VOID newdot(nap)
1114 * area * nap pointer to the new area structure
1116 * The function newdot():
1117 * (1) copies the current values of fuzz and the last
1118 * address into the current area referenced by dot
1119 * (2) loads dot with the pointer to the new area and
1120 * loads the fuzz and last address parameters
1121 * (3) outall() is called to flush any remaining
1122 * bufferred code from the old area to the output
1125 * area * oap pointer to old area
1128 * sym dot defined as sym[0]
1129 * Addr_T fuzz tracks pass to pass changes in the
1130 * address of symbols caused by
1131 * variable length instruction formats
1137 * Current area saved, new area loaded, buffers flushed.
1141 newdot(struct area *nap)
1143 register struct area *oap;
1147 oap->a_size = dot.s_addr;
1150 dot.s_addr = nap->a_size;
1154 /*)Function VOID phase(ap, a)
1156 * area * ap pointer to area
1157 * Addr_T a address in area
1159 * Function phase() compares the area ap and address a
1160 * with the current area dot.s_area and address dot.s_addr
1161 * to determine if the position of the symbol has changed
1162 * between assembler passes.
1168 * sym * dot defined as sym[0]
1174 * The p error is invoked if the area and/or address
1179 phase(struct area *ap, Addr_T a)
1181 if (ap != dot.s_area || a != dot.s_addr)
1187 "Usage: [-dqxjgalopscf] outfile file1 [file2 file3 ...]",
1189 "Usage: [-dqxjgalopscf] file1 [file2 file3 ...]",
1191 " d decimal listing",
1193 " x hex listing (default)",
1194 " j add line number and debug information to file", /* JLH */
1195 " g undefined symbols made global",
1196 " a all user symbols made global",
1198 " l create list output outfile[LST]",
1199 " o create object output outfile[o]",
1200 " s create symbol output outfile[SYM]",
1202 " l create list output file1[LST]",
1203 " o create object output file1[REL]",
1204 " s create symbol output file1[SYM]",
1206 " c generate sdcdb debug information",
1207 " p disable listing pagination",
1208 " f flag relocatable references by ` in listing file",
1209 " ff flag relocatable references by mode in listing file",
1214 /*)Function VOID usage()
1216 * The function usage() outputs to the stderr device the
1217 * assembler name and version and a list of valid assembler options.
1220 * char ** dp pointer to an array of
1221 * text string pointers.
1224 * char cpu[] assembler type string
1225 * char * usetxt[] array of string pointers
1228 * VOID asexit() asmain.c
1229 * int fprintf() c_library
1232 * program is terminated
1240 fprintf(stderr, "\nASxxxx Assembler %s (%s)\n\n", VERSION, cpu);
1241 for (dp = usetxt; *dp; dp++)
1242 fprintf(stderr, "%s\n", *dp);