4 * (C) Copyright 1989-1995
11 * 29-Oct-97 JLH pass ";!" comments to output file
24 * The module asmain.c includes the command argument parser,
25 * the three pass sequencer, and the machine independent
26 * assembler parsing code.
28 * asmain.c contains the following functions:
29 * VOID main(argc, argv)
32 * FILE * afile(fn, ft, wf)
37 * asmain.c contains the array char *usetxt[] which
38 * references the usage text strings printed by usage().
41 /*)Function VOID main(argc, argv)
43 * int argc argument count
44 * char * argv array of pointers to argument strings
46 * The function main() is the entry point to the assembler.
47 * The purpose of main() is to (1) parse the command line
48 * arguments for options and source file specifications and
49 * (2) to process the source files through the 3 pass assembler.
50 * Before each assembler pass various variables are initialized
51 * and source files are rewound to their beginning. During each
52 * assembler pass each assembler-source text line is processed.
53 * After each assembler pass the assembler information is flushed
54 * to any opened output files and the if-else-endif processing
55 * is checked for proper termination.
57 * The function main() is also responsible for opening all
58 * output files (REL, LST, and SYM), sequencing the global (-g)
59 * and all-global (-a) variable definitions, and dumping the
60 * REL file header information.
63 * char * p pointer to argument string
64 * int c character from argument string
65 * int i argument loop counter
66 * area * ap pointer to area structure
69 * int aflag -a, make all symbols global flag
70 * char afn[] afile() constructed filespec
71 * area * areap pointer to an area structure
72 * int cb[] array of assembler output values
73 * int cbt[] array of assembler relocation types
74 * describing the data in cb[]
75 * int cfile current file handle index
76 * of input assembly files
77 * int * cp pointer to assembler output array cb[]
78 * int * cpt pointer to assembler relocation type
80 * char eb[] array of generated error codes
81 * char * ep pointer into error list array eb[]
82 * int fflag -f(f), relocations flagged flag
83 * int flevel IF-ELSE-ENDIF flag will be non
84 * zero for false conditional case
85 * Addr_T fuzz tracks pass to pass changes in the
86 * address of symbols caused by
87 * variable length instruction formats
88 * int gflag -g, make undefined symbols global flag
89 * char ib[] assembler-source text line
90 * int inpfil count of assembler
91 * input files specified
92 * int ifcnd[] array of IF statement condition
93 * values (0 = FALSE) indexed by tlevel
94 * int iflvl[] array of IF-ELSE-ENDIF flevel
95 * values indexed by tlevel
96 * int incfil current file handle index
98 * char * ip pointer into the assembler-source
100 * jmp_buf jump_env compiler dependent structure
101 * used by setjmp() and longjmp()
102 * int lflag -l, generate listing flag
103 * int line current assembler source
105 * int lop current line number on page
106 * int oflag -o, generate relocatable output flag
107 * int jflag -j, generate debug info flag
108 * int page current page number
109 * int pflag enable listing pagination
110 * int pass assembler pass number
111 * int radix current number conversion radix:
112 * 2 (binary), 8 (octal), 10 (decimal),
114 * int sflag -s, generate symbol table flag
115 * char srcfn[][] array of source file names
116 * int srcline[] current source file line
117 * char stb[] Subtitle string buffer
118 * sym * symp pointer to a symbol structure
119 * int tlevel current conditional level
120 * int xflag -x, listing radix flag
121 * FILE * lfp list output file handle
122 * FILE * ofp relocation output file handle
123 * FILE * tfp symbol table output file handle
124 * FILE * sfp[] array of assembler-source file handles
127 * FILE * afile() asmain.c
128 * VOID allglob() assym.c
129 * VOID asexit() asmain.c
130 * VOID diag() assubr.c
131 * VOID err() assubr.c
132 * int fprintf() c-library
133 * int getline() aslex.c
134 * VOID list() aslist.c
135 * VOID lstsym() aslist.c
136 * VOID minit() ___mch.c
137 * VOID newdot() asmain.c
138 * VOID outchk() asout.c
139 * VOID outgsd() asout.c
140 * int rewind() c-library
141 * int setjmp() c-library
142 * VOID symglob() assym.c
143 * VOID syminit() assym.c
144 * VOID usage() asmain.c
147 * Completion of main() completes the assembly process.
148 * REL, LST, and/or SYM files may be generated.
162 /*fprintf(stdout, "\n");*/
165 for (i=1; i<argc; ++i) {
171 while ((c = *p++) != 0)
189 case 'j': /* JLH: debug info */
192 ++oflag; /* force object */
239 if (++inpfil == MAXFIL) {
240 fprintf(stderr, "too many input files\n");
243 sfp[inpfil] = afile(p, "", 0);
244 strcpy(srcfn[inpfil],afn);
247 lfp = afile(p, "lst", 1);
249 ofp = afile(p, "rel", 1);
250 // save the file name if we have to delete it on error
254 tfp = afile(p, "sym", 1);
261 for (pass=0; pass<3; ++pass) {
262 if (gflag && pass == 1)
264 if (aflag && pass == 1)
266 if (oflag && pass == 2)
279 for (i = 0; i <= inpfil; i++)
298 /* JLH: if line begins with ";!", then
299 * pass this comment on to the output file
301 if (oflag && (pass == 1) &&
302 (ip[0] == ';') && (ip[1] == '!'))
304 fprintf(ofp, "%s\n", ip );
307 if (setjmp(jump_env) == 0)
315 newdot(dot.s_area); /* Flush area info */
316 if (flevel || tlevel)
320 outchk(HUGE, HUGE); /* Flush */
327 //printf ("aserr: %d\n", aserr);
328 //printf ("fatalErrors: %d\n", fatalErrors);
330 return 0; // hush the compiler
333 /*)Function VOID asexit(i)
337 * The function asexit() explicitly closes all open
338 * files and then terminates the program.
344 * FILE * ifp[] array of include-file file handles
345 * FILE * lfp list output file handle
346 * FILE * ofp relocation output file handle
347 * FILE * tfp symbol table output file handle
348 * FILE * sfp[] array of assembler-source file handles
351 * int fclose() c-library
352 * VOID exit() c-library
355 * All files closed. Program terminates.
364 if (lfp != NULL) fclose(lfp);
365 if (ofp != NULL) fclose(ofp);
366 if (tfp != NULL) fclose(tfp);
368 for (j=0; j<MAXFIL && sfp[j] != NULL; j++) {
372 /*for (j=0; j<MAXINC && ifp[j] != NULL; j++) {
376 // remove output file
377 printf ("removing %s\n", relFile);
383 /*)Function VOID asmbl()
385 * The function asmbl() scans the assembler-source text for
386 * (1) labels, global labels, equates, global equates, and local
387 * symbols, (2) .if, .else, .endif, and .page directives,
388 * (3) machine independent assembler directives, and (4) machine
389 * dependent mnemonics.
392 * mne * mp pointer to a mne structure
393 * sym * sp pointer to a sym structure
394 * tsym * tp pointer to a tsym structure
395 * int c character from assembler-source
397 * area * ap pointer to an area structure
398 * expr e1 expression structure
399 * char id[] id string
400 * char opt[] options string
401 * char fn[] filename string
402 * char * p pointer into a string
403 * int d temporary value
404 * int n temporary value
405 * int uaf user area options flag
406 * int uf area options
409 * area * areap pointer to an area structure
410 * char ctype[] array of character types, one per
412 * int flevel IF-ELSE-ENDIF flag will be non
413 * zero for false conditional case
414 * Addr_T fuzz tracks pass to pass changes in the
415 * address of symbols caused by
416 * variable length instruction formats
417 * int ifcnd[] array of IF statement condition
418 * values (0 = FALSE) indexed by tlevel
419 * int iflvl[] array of IF-ELSE-ENDIF flevel
420 * values indexed by tlevel
421 * FILE * ifp[] array of include-file file handles
422 * char incfn[][] array of include file names
423 * int incline[] current include file line
424 * int incfil current file handle index
426 * Addr_T laddr address of current assembler line
427 * or value of .if argument
428 * int lmode listing mode
429 * int lop current line number on page
430 * char module[] module name string
431 * int pass assembler pass number
432 * int radix current number conversion radix:
433 * 2 (binary), 8 (octal), 10 (decimal),
435 * char stb[] Subtitle string buffer
436 * sym * symp pointer to a symbol structure
437 * char tb[] Title string buffer
438 * int tlevel current conditional level
441 * Addr_T absexpr() asexpr.c
442 * area * alookup() assym.c
443 * VOID clrexpr() asexpr.c
444 * int digit() asexpr.c
445 * char endline() aslex.c
446 * VOID err() assubr.c
447 * VOID expr() asexpr.c
448 * FILE * fopen() c-library
450 * VOID getid() aslex.c
451 * int getmap() aslex.c
452 * char getnb() aslex.c
453 * VOID getst() aslex.c
454 * sym * lookup() assym.c
455 * VOID machine() ___mch.c
456 * mne * mlookup() assym.c
458 * VOID * new() assym.c
459 * VOID newdot() asmain.c
460 * VOID outall() asout.c
461 * VOID outab() asout.c
462 * VOID outchk() asout.c
463 * VOID outrb() asout.c
464 * VOID outrw() asout.c
465 * VOID phase() asmain.c
466 * VOID qerr() assubr.c
467 * char * strcpy() c-library
468 * char * strncpy() c-library
469 * VOID unget() aslex.c
475 register struct mne *mp;
476 register struct sym *sp;
477 register struct tsym *tp;
490 if ((c=endline()) == 0) { return; }
492 * If the first character is a digit then assume
493 * a local symbol is being specified. The symbol
494 * must end with $: to be valid.
496 * Construct a tsym structure at the first
497 * occurance of the symbol. Flag the symbol
498 * as multiply defined if not the first occurance.
500 * Load area, address, and fuzz values
501 * into structure tsym.
503 * Check for assembler phase error and
504 * multiply defined error.
506 if (ctype[c] & DIGIT) {
510 while ((d = digit(c, 10)) >= 0) {
514 if (c != '$' || get() != ':')
519 if (n == tp->t_num) {
526 tp=(struct tsym *) new (sizeof(struct tsym));
527 tp->t_lnk = symp->s_tsym;
530 tp->t_area = dot.s_area;
531 tp->t_addr = dot.s_addr;
536 if (n == tp->t_num) {
543 fuzz = tp->t_addr - dot.s_addr;
544 tp->t_area = dot.s_area;
545 tp->t_addr = dot.s_addr;
547 phase(tp->t_area, tp->t_addr);
548 if (tp->t_flg & S_MDF)
559 * If the first character is a letter then assume a lable,
560 * symbol, assembler directive, or assembler mnemonic is
563 if ((ctype[c] & LETTER) == 0) {
573 * If the next character is a : then a label is being processed.
574 * A double :: defines a global label. If this is new label
575 * then create a symbol structure.
577 * Flag multiply defined labels.
579 * Load area, address, and fuzz values
580 * into structure symp.
582 * Check for assembler phase error and
583 * multiply defined error.
588 if ((c = get()) != ':') {
596 if ((symp->s_type != S_NEW) &&
597 ((symp->s_flag & S_ASG) == 0))
598 symp->s_flag |= S_MDF;
600 fuzz = symp->s_addr - dot.s_addr;
601 symp->s_type = S_USER;
602 symp->s_area = dot.s_area;
603 symp->s_addr = dot.s_addr;
605 if (symp->s_flag & S_MDF)
607 phase(symp->s_area, symp->s_addr);
610 symp->s_flag |= S_GBL;
616 * If the next character is a = then an equate is being processed.
617 * A double == defines a global equate. If this is new variable
618 * then create a symbol structure.
623 if ((c = get()) != '=') {
632 if (e1.e_flag || e1.e_base.e_ap != dot.s_area)
635 if (sp->s_type != S_NEW && (sp->s_flag & S_ASG) == 0) {
639 sp->s_area = e1.e_base.e_ap;
640 sp->s_addr = laddr = e1.e_addr;
649 lmode = flevel ? SLIST : CLIST;
650 if ((mp = mlookup(id)) == NULL) {
656 * If we have gotten this far then we have found an
657 * assembler directive or an assembler mnemonic.
659 * Check for .if, .else, .endif, and .page directives
660 * which are not controlled by the conditional flags
662 switch (mp->m_type) {
666 if (tlevel < MAXIF) {
669 iflvl[tlevel] = flevel;
682 if (++flevel > (iflvl[tlevel]+1)) {
686 if (--flevel < iflvl[tlevel]) {
695 flevel = iflvl[tlevel--];
713 * If we are not in a false state for .if/.else then
714 * process the assembler directives here.
716 switch (mp->m_type) {
720 laddr = dot.s_addr = (dot.s_addr + 1) & ~1;
726 laddr = dot.s_addr |= 1;
735 if (mp->m_type == S_BYTE) {
740 } while ((c = getnb()) == ',');
746 if ((d = getnb()) == '\0')
748 while ((c = getmap(d)) >= 0)
750 if (mp->m_type == S_ASCIZ)
755 if ((d = getnb()) == '\0')
759 if ((n = getmap(d)) >= 0) {
772 dot.s_addr += e1.e_addr*mp->m_valu;
778 if ((c = getnb()) != 0) {
780 if (p < &tb[NTITL-1])
782 } while ((c = get()) != 0);
791 if ((c = getnb()) != 0) {
793 if (p < &stb[NSBTL-1])
795 } while ((c = get()) != 0);
803 getst(id, getnb()); // a module can start with a digit
808 strncpy(module, id, NCPS);
819 } while ((c = getnb()) == ',');
828 if ((c = getnb()) == '(') {
832 if (mp && mp->m_type == S_ATYP) {
838 } while ((c = getnb()) == ',');
844 if ((ap = alookup(id)) != NULL) {
845 if (uaf && uf != ap->a_flag)
848 ap = (struct area *) new (sizeof(struct area));
850 strncpy(ap->a_id, id, NCPS);
851 ap->a_ref = areap->a_ref + 1;
854 ap->a_flag = uaf ? uf : (A_CON|A_REL);
862 if (dot.s_area->a_flag & A_ABS) {
864 laddr = dot.s_addr = absexpr();
910 while ((c = get()) != d) {
911 if (p < &fn[PATH_MAX-1]) {
918 if (++incfil == MAXINC ||
919 (ifp[incfil] = fopen(fn, "r")) == NULL) {
925 strcpy(incfn[incfil],fn);
935 if (!as_strcmpi(id, "on"))
937 /* Quick sanity check: size of
938 * Addr_T must be at least 24 bits.
940 if (sizeof(Addr_T) < 3)
944 "Cannot enable Flat24 mode: "
945 "host system must have 24 bit "
946 "or greater integers.\n");
953 else if (!as_strcmpi(id, "off"))
968 printf("as8051: ds390 flat mode %sabled.\n",
969 flat24Mode ? "en" : "dis");
975 * If not an assembler directive then go to
976 * the machine dependent function which handles
977 * all the assembler mnemonics.
981 /* if cdb information the generate the line info */
982 if (cflag && (pass == 1))
985 /* JLH: if -j, generate a line number symbol */
986 if (jflag && (pass == 1))
995 /*)Function FILE * afile(fn, ft, wf)
997 * char * fn file specification string
998 * char * ft file type string
999 * int wf read(0)/write(1) flag
1001 * The function afile() opens a file for reading or writing.
1002 * (1) If the file type specification string ft
1003 * is not NULL then a file specification is
1004 * constructed with the file path\name in fn
1005 * and the extension in ft.
1006 * (2) If the file type specification string ft
1007 * is NULL then the file specification is
1008 * constructed from fn. If fn does not have
1009 * a file type then the default source file
1010 * type dsft is appended to the file specification.
1012 * afile() returns a file handle for the opened file or aborts
1013 * the assembler on an open error.
1016 * int c character value
1017 * FILE * fp filehandle for opened file
1018 * char * p1 pointer to filespec string fn
1019 * char * p2 pointer to filespec string fb
1020 * char * p3 pointer to filetype string ft
1023 * char afn[] afile() constructed filespec
1024 * char dsft[] default assembler file type string
1025 * char afn[] constructed file specification string
1028 * VOID asexit() asmain.c
1029 * FILE * fopen() c_library
1030 * int fprintf() c_library
1033 * File is opened for read or write.
1042 register char *p2, *p3;
1050 p2 = strrchr (afn, FSEPX); // search last '.'
1052 p2 = afn + strlen (afn);
1053 if (p2 > &afn[PATH_MAX-4]) // truncate filename, if it's too long
1054 p2 = &afn[PATH_MAX-4];
1057 // choose a file-extension
1058 if (*p3 == 0) { // extension supplied?
1059 p3 = strrchr (fn, FSEPX); // no: extension in fn?
1063 p3 = dsft; // no: default extension
1066 while ((c = *p3++) != 0) { // strncpy
1067 if (p2 < &afn[PATH_MAX-1])
1072 if ((fp = fopen(afn, wf?"w":"r")) == NULL) {
1073 fprintf(stderr, "%s: cannot %s.\n", afn, wf?"create":"open");
1079 /*)Function VOID newdot(nap)
1081 * area * nap pointer to the new area structure
1083 * The function newdot():
1084 * (1) copies the current values of fuzz and the last
1085 * address into the current area referenced by dot
1086 * (2) loads dot with the pointer to the new area and
1087 * loads the fuzz and last address parameters
1088 * (3) outall() is called to flush any remaining
1089 * bufferred code from the old area to the output
1092 * area * oap pointer to old area
1095 * sym dot defined as sym[0]
1096 * Addr_T fuzz tracks pass to pass changes in the
1097 * address of symbols caused by
1098 * variable length instruction formats
1104 * Current area saved, new area loaded, buffers flushed.
1109 register struct area *nap;
1111 register struct area *oap;
1114 /* fprintf (stderr, "%s dot.s_area->a_size: %d dot.s_addr: %d\n",
1115 oap->a_id, dot.s_area->a_size, dot.s_addr); */
1117 if (oap->a_flag & A_OVR) {
1118 // the size of an overlay is the biggest size encountered
1119 if (oap->a_size < dot.s_addr) {
1120 oap->a_size = dot.s_addr;
1123 oap->a_size = dot.s_addr;
1125 if (nap->a_flag & A_OVR) {
1126 // a new overlay starts at 0, no fuzz
1130 dot.s_addr = nap->a_size;
1137 /*)Function VOID phase(ap, a)
1139 * area * ap pointer to area
1140 * Addr_T a address in area
1142 * Function phase() compares the area ap and address a
1143 * with the current area dot.s_area and address dot.s_addr
1144 * to determine if the position of the symbol has changed
1145 * between assembler passes.
1151 * sym * dot defined as sym[0]
1157 * The p error is invoked if the area and/or address
1166 if (ap != dot.s_area || a != dot.s_addr)
1171 "Usage: [-dqxjgalopsf] file1 [file2 file3 ...]",
1172 " d decimal listing",
1174 " x hex listing (default)",
1175 " j add line number and debug information to file", /* JLH */
1176 " g undefined symbols made global",
1177 " a all user symbols made global",
1178 " l create list output file1[LST]",
1179 " o create object output file1[REL]",
1180 " s create symbol output file1[SYM]",
1181 " p disable listing pagination",
1182 " f flag relocatable references by ` in listing file",
1183 " ff flag relocatable references by mode in listing file",
1188 /*)Function VOID usage()
1190 * The function usage() outputs to the stderr device the
1191 * assembler name and version and a list of valid assembler options.
1194 * char ** dp pointer to an array of
1195 * text string pointers.
1198 * char cpu[] assembler type string
1199 * char * usetxt[] array of string pointers
1202 * VOID asexit() asmain.c
1203 * int fprintf() c_library
1206 * program is terminated
1214 fprintf(stderr, "\nASxxxx Assembler %s (%s)\n\n", VERSION, cpu);
1215 for (dp = usetxt; *dp; dp++)
1216 fprintf(stderr, "%s\n", *dp);