4 * (C) Copyright 1989-1995
11 * 29-Oct-97 JLH pass ";!" comments to output file
23 * The module asmain.c includes the command argument parser,
24 * the three pass sequencer, and the machine independent
25 * assembler parsing code.
27 * asmain.c contains the following functions:
28 * VOID main(argc, argv)
31 * FILE * afile(fn, ft, wf)
36 * asmain.c contains the array char *usetxt[] which
37 * references the usage text strings printed by usage().
40 /*)Function VOID main(argc, argv)
42 * int argc argument count
43 * char * argv array of pointers to argument strings
45 * The function main() is the entry point to the assembler.
46 * The purpose of main() is to (1) parse the command line
47 * arguments for options and source file specifications and
48 * (2) to process the source files through the 3 pass assembler.
49 * Before each assembler pass various variables are initialized
50 * and source files are rewound to their beginning. During each
51 * assembler pass each assembler-source text line is processed.
52 * After each assembler pass the assembler information is flushed
53 * to any opened output files and the if-else-endif processing
54 * is checked for proper termination.
56 * The function main() is also responsible for opening all
57 * output files (REL, LST, and SYM), sequencing the global (-g)
58 * and all-global (-a) variable definitions, and dumping the
59 * REL file header information.
62 * char * p pointer to argument string
63 * int c character from argument string
64 * int i argument loop counter
65 * area * ap pointer to area structure
68 * int aflag -a, make all symbols global flag
69 * char afn[] afile() constructed filespec
70 * area * areap pointer to an area structure
71 * int cb[] array of assembler output values
72 * int cbt[] array of assembler relocation types
73 * describing the data in cb[]
74 * int cfile current file handle index
75 * of input assembly files
76 * int * cp pointer to assembler output array cb[]
77 * int * cpt pointer to assembler relocation type
79 * char eb[] array of generated error codes
80 * char * ep pointer into error list array eb[]
81 * int fflag -f(f), relocations flagged flag
82 * int flevel IF-ELSE-ENDIF flag will be non
83 * zero for false conditional case
84 * Addr_T fuzz tracks pass to pass changes in the
85 * address of symbols caused by
86 * variable length instruction formats
87 * int gflag -g, make undefined symbols global flag
88 * char ib[] assembler-source text line
89 * int inpfil count of assembler
90 * input files specified
91 * int ifcnd[] array of IF statement condition
92 * values (0 = FALSE) indexed by tlevel
93 * int iflvl[] array of IF-ELSE-ENDIF flevel
94 * values indexed by tlevel
95 * int incfil current file handle index
97 * char * ip pointer into the assembler-source
99 * jmp_buf jump_env compiler dependent structure
100 * used by setjmp() and longjmp()
101 * int lflag -l, generate listing flag
102 * int line current assembler source
104 * int lop current line number on page
105 * int oflag -o, generate relocatable output flag
106 * int jflag -j, generate debug info flag
107 * int page current page number
108 * int pflag enable listing pagination
109 * int pass assembler pass number
110 * int radix current number conversion radix:
111 * 2 (binary), 8 (octal), 10 (decimal),
113 * int sflag -s, generate symbol table flag
114 * char srcfn[][] array of source file names
115 * int srcline[] current source file line
116 * char stb[] Subtitle string buffer
117 * sym * symp pointer to a symbol structure
118 * int tlevel current conditional level
119 * int xflag -x, listing radix flag
120 * FILE * lfp list output file handle
121 * FILE * ofp relocation output file handle
122 * FILE * tfp symbol table output file handle
123 * FILE * sfp[] array of assembler-source file handles
126 * FILE * afile() asmain.c
127 * VOID allglob() assym.c
128 * VOID asexit() asmain.c
129 * VOID diag() assubr.c
130 * VOID err() assubr.c
131 * int fprintf() c-library
132 * int getline() aslex.c
133 * VOID list() aslist.c
134 * VOID lstsym() aslist.c
135 * VOID minit() ___mch.c
136 * VOID newdot() asmain.c
137 * VOID outchk() asout.c
138 * VOID outgsd() asout.c
139 * int rewind() c-library
140 * int setjmp() c-library
141 * VOID symglob() assym.c
142 * VOID syminit() assym.c
143 * VOID usage() asmain.c
146 * Completion of main() completes the assembly process.
147 * REL, LST, and/or SYM files may be generated.
161 /*fprintf(stdout, "\n");*/
164 for (i=1; i<argc; ++i) {
170 while ((c = *p++) != 0)
188 case 'j': /* JLH: debug info */
191 ++oflag; /* force object */
238 if (++inpfil == MAXFIL) {
239 fprintf(stderr, "too many input files\n");
242 sfp[inpfil] = afile(p, "", 0);
243 strcpy(srcfn[inpfil],afn);
246 lfp = afile(p, "lst", 1);
248 ofp = afile(p, "rel", 1);
249 // save the file name if we have to delete it on error
253 tfp = afile(p, "sym", 1);
260 for (pass=0; pass<3; ++pass) {
261 if (gflag && pass == 1)
263 if (aflag && pass == 1)
265 if (oflag && pass == 2)
278 for (i = 0; i <= inpfil; i++)
297 /* JLH: if line begins with ";!", then
298 * pass this comment on to the output file
300 if (oflag && (pass == 1) &&
301 (ip[0] == ';') && (ip[1] == '!'))
303 fprintf(ofp, "%s\n", ip );
306 if (setjmp(jump_env) == 0)
314 newdot(dot.s_area); /* Flush area info */
315 if (flevel || tlevel)
319 outchk(HUGE, HUGE); /* Flush */
326 //printf ("aserr: %d\n", aserr);
327 //printf ("fatalErrors: %d\n", fatalErrors);
329 return 0; // hush the compiler
332 /*)Function VOID asexit(i)
336 * The function asexit() explicitly closes all open
337 * files and then terminates the program.
343 * FILE * ifp[] array of include-file file handles
344 * FILE * lfp list output file handle
345 * FILE * ofp relocation output file handle
346 * FILE * tfp symbol table output file handle
347 * FILE * sfp[] array of assembler-source file handles
350 * int fclose() c-library
351 * VOID exit() c-library
354 * All files closed. Program terminates.
363 if (lfp != NULL) fclose(lfp);
364 if (ofp != NULL) fclose(ofp);
365 if (tfp != NULL) fclose(tfp);
367 for (j=0; j<MAXFIL && sfp[j] != NULL; j++) {
371 /*for (j=0; j<MAXINC && ifp[j] != NULL; j++) {
375 // remove output file
376 printf ("removing %s\n", relFile);
382 /*)Function VOID asmbl()
384 * The function asmbl() scans the assembler-source text for
385 * (1) labels, global labels, equates, global equates, and local
386 * symbols, (2) .if, .else, .endif, and .page directives,
387 * (3) machine independent assembler directives, and (4) machine
388 * dependent mnemonics.
391 * mne * mp pointer to a mne structure
392 * sym * sp pointer to a sym structure
393 * tsym * tp pointer to a tsym structure
394 * int c character from assembler-source
396 * area * ap pointer to an area structure
397 * expr e1 expression structure
398 * char id[] id string
399 * char opt[] options string
400 * char fn[] filename string
401 * char * p pointer into a string
402 * int d temporary value
403 * int n temporary value
404 * int uaf user area options flag
405 * int uf area options
408 * area * areap pointer to an area structure
409 * char ctype[] array of character types, one per
411 * int flevel IF-ELSE-ENDIF flag will be non
412 * zero for false conditional case
413 * Addr_T fuzz tracks pass to pass changes in the
414 * address of symbols caused by
415 * variable length instruction formats
416 * int ifcnd[] array of IF statement condition
417 * values (0 = FALSE) indexed by tlevel
418 * int iflvl[] array of IF-ELSE-ENDIF flevel
419 * values indexed by tlevel
420 * FILE * ifp[] array of include-file file handles
421 * char incfn[][] array of include file names
422 * int incline[] current include file line
423 * int incfil current file handle index
425 * Addr_T laddr address of current assembler line
426 * or value of .if argument
427 * int lmode listing mode
428 * int lop current line number on page
429 * char module[] module name string
430 * int pass assembler pass number
431 * int radix current number conversion radix:
432 * 2 (binary), 8 (octal), 10 (decimal),
434 * char stb[] Subtitle string buffer
435 * sym * symp pointer to a symbol structure
436 * char tb[] Title string buffer
437 * int tlevel current conditional level
440 * Addr_T absexpr() asexpr.c
441 * area * alookup() assym.c
442 * VOID clrexpr() asexpr.c
443 * int digit() asexpr.c
444 * char endline() aslex.c
445 * VOID err() assubr.c
446 * VOID expr() asexpr.c
447 * FILE * fopen() c-library
449 * VOID getid() aslex.c
450 * int getmap() aslex.c
451 * char getnb() aslex.c
452 * VOID getst() aslex.c
453 * sym * lookup() assym.c
454 * VOID machine() ___mch.c
455 * mne * mlookup() assym.c
457 * VOID * new() assym.c
458 * VOID newdot() asmain.c
459 * VOID outall() asout.c
460 * VOID outab() asout.c
461 * VOID outchk() asout.c
462 * VOID outrb() asout.c
463 * VOID outrw() asout.c
464 * VOID phase() asmain.c
465 * VOID qerr() assubr.c
466 * char * strcpy() c-library
467 * char * strncpy() c-library
468 * VOID unget() aslex.c
474 register struct mne *mp;
475 register struct sym *sp;
476 register struct tsym *tp;
489 if ((c=endline()) == 0) { return; }
491 * If the first character is a digit then assume
492 * a local symbol is being specified. The symbol
493 * must end with $: to be valid.
495 * Construct a tsym structure at the first
496 * occurance of the symbol. Flag the symbol
497 * as multiply defined if not the first occurance.
499 * Load area, address, and fuzz values
500 * into structure tsym.
502 * Check for assembler phase error and
503 * multiply defined error.
505 if (ctype[c] & DIGIT) {
509 while ((d = digit(c, 10)) >= 0) {
513 if (c != '$' || get() != ':')
518 if (n == tp->t_num) {
525 tp=(struct tsym *) new (sizeof(struct tsym));
526 tp->t_lnk = symp->s_tsym;
529 tp->t_area = dot.s_area;
530 tp->t_addr = dot.s_addr;
535 if (n == tp->t_num) {
542 fuzz = tp->t_addr - dot.s_addr;
543 tp->t_area = dot.s_area;
544 tp->t_addr = dot.s_addr;
546 phase(tp->t_area, tp->t_addr);
547 if (tp->t_flg & S_MDF)
558 * If the first character is a letter then assume a lable,
559 * symbol, assembler directive, or assembler mnemonic is
562 if ((ctype[c] & LETTER) == 0) {
572 * If the next character is a : then a label is being processed.
573 * A double :: defines a global label. If this is new label
574 * then create a symbol structure.
576 * Flag multiply defined labels.
578 * Load area, address, and fuzz values
579 * into structure symp.
581 * Check for assembler phase error and
582 * multiply defined error.
587 if ((c = get()) != ':') {
595 if ((symp->s_type != S_NEW) &&
596 ((symp->s_flag & S_ASG) == 0))
597 symp->s_flag |= S_MDF;
599 fuzz = symp->s_addr - dot.s_addr;
600 symp->s_type = S_USER;
601 symp->s_area = dot.s_area;
602 symp->s_addr = dot.s_addr;
604 if (symp->s_flag & S_MDF)
606 phase(symp->s_area, symp->s_addr);
609 symp->s_flag |= S_GBL;
615 * If the next character is a = then an equate is being processed.
616 * A double == defines a global equate. If this is new variable
617 * then create a symbol structure.
622 if ((c = get()) != '=') {
631 if (e1.e_flag || e1.e_base.e_ap != dot.s_area)
634 if (sp->s_type != S_NEW && (sp->s_flag & S_ASG) == 0) {
638 sp->s_area = e1.e_base.e_ap;
639 sp->s_addr = laddr = e1.e_addr;
648 lmode = flevel ? SLIST : CLIST;
649 if ((mp = mlookup(id)) == NULL) {
655 * If we have gotten this far then we have found an
656 * assembler directive or an assembler mnemonic.
658 * Check for .if, .else, .endif, and .page directives
659 * which are not controlled by the conditional flags
661 switch (mp->m_type) {
665 if (tlevel < MAXIF) {
668 iflvl[tlevel] = flevel;
681 if (++flevel > (iflvl[tlevel]+1)) {
685 if (--flevel < iflvl[tlevel]) {
694 flevel = iflvl[tlevel--];
712 * If we are not in a false state for .if/.else then
713 * process the assembler directives here.
715 switch (mp->m_type) {
719 laddr = dot.s_addr = (dot.s_addr + 1) & ~1;
725 laddr = dot.s_addr |= 1;
734 if (mp->m_type == S_BYTE) {
739 } while ((c = getnb()) == ',');
745 if ((d = getnb()) == '\0')
747 while ((c = getmap(d)) >= 0)
749 if (mp->m_type == S_ASCIZ)
754 if ((d = getnb()) == '\0')
758 if ((n = getmap(d)) >= 0) {
771 dot.s_addr += e1.e_addr*mp->m_valu;
777 if ((c = getnb()) != 0) {
779 if (p < &tb[NTITL-1])
781 } while ((c = get()) != 0);
790 if ((c = getnb()) != 0) {
792 if (p < &stb[NSBTL-1])
794 } while ((c = get()) != 0);
807 strncpy(module, id, NCPS);
818 } while ((c = getnb()) == ',');
827 if ((c = getnb()) == '(') {
831 if (mp && mp->m_type == S_ATYP) {
837 } while ((c = getnb()) == ',');
843 if ((ap = alookup(id)) != NULL) {
844 if (uaf && uf != ap->a_flag)
846 if (ap->a_flag & A_OVR) {
851 ap = (struct area *) new (sizeof(struct area));
853 strncpy(ap->a_id, id, NCPS);
854 ap->a_ref = areap->a_ref + 1;
857 ap->a_flag = uaf ? uf : (A_CON|A_REL);
865 if (dot.s_area->a_flag & A_ABS) {
867 laddr = dot.s_addr = absexpr();
913 while ((c = get()) != d) {
914 if (p < &fn[FILSPC-1]) {
921 if (++incfil == MAXINC ||
922 (ifp[incfil] = fopen(fn, "r")) == NULL) {
928 strcpy(incfn[incfil],fn);
938 if (!strcmpi(id, "on"))
940 /* Quick sanity check: size of
941 * Addr_T must be at least 24 bits.
943 if (sizeof(Addr_T) < 3)
947 "Cannot enable Flat24 mode: "
948 "host system must have 24 bit "
949 "or greater integers.\n");
956 else if (!strcmpi(id, "off"))
971 printf("as8051: ds390 flat mode %sabled.\n",
972 flat24Mode ? "en" : "dis");
978 * If not an assembler directive then go to
979 * the machine dependent function which handles
980 * all the assembler mnemonics.
984 /* if cdb information the generate the line info */
985 if (cflag && (pass == 1))
988 /* JLH: if -j, generate a line number symbol */
989 if (jflag && (pass == 1))
998 /*)Function FILE * afile(fn, ft, wf)
1000 * char * fn file specification string
1001 * char * ft file type string
1002 * int wf read(0)/write(1) flag
1004 * The function afile() opens a file for reading or writing.
1005 * (1) If the file type specification string ft
1006 * is not NULL then a file specification is
1007 * constructed with the file path\name in fn
1008 * and the extension in ft.
1009 * (2) If the file type specification string ft
1010 * is NULL then the file specification is
1011 * constructed from fn. If fn does not have
1012 * a file type then the default source file
1013 * type dsft is appended to the file specification.
1015 * afile() returns a file handle for the opened file or aborts
1016 * the assembler on an open error.
1019 * int c character value
1020 * FILE * fp filehandle for opened file
1021 * char * p1 pointer to filespec string fn
1022 * char * p2 pointer to filespec string fb
1023 * char * p3 pointer to filetype string ft
1026 * char afn[] afile() constructed filespec
1027 * char dsft[] default assembler file type string
1028 * char afn[] constructed file specification string
1031 * VOID asexit() asmain.c
1032 * FILE * fopen() c_library
1033 * int fprintf() c_library
1036 * File is opened for read or write.
1045 register char *p1, *p2, *p3;
1052 while ((c = *p1++) != 0 && c != FSEPX) {
1053 if (p2 < &afn[FILSPC-4])
1064 while ((c = *p3++) != 0) {
1065 if (p2 < &afn[FILSPC-1])
1069 if ((fp = fopen(afn, wf?"w":"r")) == NULL) {
1070 fprintf(stderr, "%s: cannot %s.\n", afn, wf?"create":"open");
1076 /*)Function VOID newdot(nap)
1078 * area * nap pointer to the new area structure
1080 * The function newdot():
1081 * (1) copies the current values of fuzz and the last
1082 * address into the current area referenced by dot
1083 * (2) loads dot with the pointer to the new area and
1084 * loads the fuzz and last address parameters
1085 * (3) outall() is called to flush any remaining
1086 * bufferred code from the old area to the output
1089 * area * oap pointer to old area
1092 * sym dot defined as sym[0]
1093 * Addr_T fuzz tracks pass to pass changes in the
1094 * address of symbols caused by
1095 * variable length instruction formats
1101 * Current area saved, new area loaded, buffers flushed.
1106 register struct area *nap;
1108 register struct area *oap;
1112 oap->a_size = dot.s_addr;
1115 dot.s_addr = nap->a_size;
1119 /*)Function VOID phase(ap, a)
1121 * area * ap pointer to area
1122 * Addr_T a address in area
1124 * Function phase() compares the area ap and address a
1125 * with the current area dot.s_area and address dot.s_addr
1126 * to determine if the position of the symbol has changed
1127 * between assembler passes.
1133 * sym * dot defined as sym[0]
1139 * The p error is invoked if the area and/or address
1148 if (ap != dot.s_area || a != dot.s_addr)
1153 "Usage: [-dqxjgalopsf] file1 [file2 file3 ...]",
1154 " d decimal listing",
1156 " x hex listing (default)",
1157 " j add line number and debug information to file", /* JLH */
1158 " g undefined symbols made global",
1159 " a all user symbols made global",
1160 " l create list output file1[LST]",
1161 " o create object output file1[REL]",
1162 " s create symbol output file1[SYM]",
1163 " p disable listing pagination",
1164 " f flag relocatable references by ` in listing file",
1165 " ff flag relocatable references by mode in listing file",
1170 /*)Function VOID usage()
1172 * The function usage() outputs to the stderr device the
1173 * assembler name and version and a list of valid assembler options.
1176 * char ** dp pointer to an array of
1177 * text string pointers.
1180 * char cpu[] assembler type string
1181 * char * usetxt[] array of string pointers
1184 * VOID asexit() asmain.c
1185 * int fprintf() c_library
1188 * program is terminated
1196 fprintf(stderr, "\nASxxxx Assembler %s (%s)\n\n", VERSION, cpu);
1197 for (dp = usetxt; *dp; dp++)
1198 fprintf(stderr, "%s\n", *dp);