4 * (C) Copyright 1989-1995
11 * 29-Oct-97 JLH pass ";!" comments to output file
17 #if !defined(_MSC_VER)
22 extern VOID machine(struct mne *);
25 * The module asmain.c includes the command argument parser,
26 * the three pass sequencer, and the machine independent
27 * assembler parsing code.
29 * asmain.c contains the following functions:
30 * VOID main(argc, argv)
33 * FILE * afile(fn, ft, wf)
38 * asmain.c contains the array char *usetxt[] which
39 * references the usage text strings printed by usage().
42 /*)Function VOID main(argc, argv)
44 * int argc argument count
45 * char * argv array of pointers to argument strings
47 * The function main() is the entry point to the assembler.
48 * The purpose of main() is to (1) parse the command line
49 * arguments for options and source file specifications and
50 * (2) to process the source files through the 3 pass assembler.
51 * Before each assembler pass various variables are initialized
52 * and source files are rewound to their beginning. During each
53 * assembler pass each assembler-source text line is processed.
54 * After each assembler pass the assembler information is flushed
55 * to any opened output files and the if-else-endif processing
56 * is checked for proper termination.
58 * The function main() is also responsible for opening all
59 * output files (REL, LST, and SYM), sequencing the global (-g)
60 * and all-global (-a) variable definitions, and dumping the
61 * REL file header information.
64 * char * p pointer to argument string
65 * int c character from argument string
66 * int i argument loop counter
67 * area * ap pointer to area structure
70 * int aflag -a, make all symbols global flag
71 * char afn[] afile() constructed filespec
72 * area * areap pointer to an area structure
73 * int cb[] array of assembler output values
74 * int cbt[] array of assembler relocation types
75 * describing the data in cb[]
76 * int cfile current file handle index
77 * of input assembly files
78 * int * cp pointer to assembler output array cb[]
79 * int * cpt pointer to assembler relocation type
81 * char eb[] array of generated error codes
82 * char * ep pointer into error list array eb[]
83 * int fflag -f(f), relocations flagged flag
84 * int flevel IF-ELSE-ENDIF flag will be non
85 * zero for false conditional case
86 * Addr_T fuzz tracks pass to pass changes in the
87 * address of symbols caused by
88 * variable length instruction formats
89 * int gflag -g, make undefined symbols global flag
90 * char ib[] assembler-source text line
91 * int inpfil count of assembler
92 * input files specified
93 * int ifcnd[] array of IF statement condition
94 * values (0 = FALSE) indexed by tlevel
95 * int iflvl[] array of IF-ELSE-ENDIF flevel
96 * values indexed by tlevel
97 * int incfil current file handle index
99 * char * ip pointer into the assembler-source
101 * jmp_buf jump_env compiler dependent structure
102 * used by setjmp() and longjmp()
103 * int lflag -l, generate listing flag
104 * int line current assembler source
106 * int lop current line number on page
107 * int oflag -o, generate relocatable output flag
108 * int jflag -j, generate debug info flag
109 * int page current page number
110 * int pflag enable listing pagination
111 * int pass assembler pass number
112 * int radix current number conversion radix:
113 * 2 (binary), 8 (octal), 10 (decimal),
115 * int sflag -s, generate symbol table flag
116 * char srcfn[][] array of source file names
117 * int srcline[] current source file line
118 * char stb[] Subtitle string buffer
119 * sym * symp pointer to a symbol structure
120 * int tlevel current conditional level
121 * int xflag -x, listing radix flag
122 * FILE * lfp list output file handle
123 * FILE * ofp relocation output file handle
124 * FILE * tfp symbol table output file handle
125 * FILE * sfp[] array of assembler-source file handles
128 * FILE * afile() asmain.c
129 * VOID allglob() assym.c
130 * VOID asexit() asmain.c
131 * VOID diag() assubr.c
132 * VOID err() assubr.c
133 * int fprintf() c-library
134 * int getline() aslex.c
135 * VOID list() aslist.c
136 * VOID lstsym() aslist.c
137 * VOID minit() ___mch.c
138 * VOID newdot() asmain.c
139 * VOID outchk() asout.c
140 * VOID outgsd() asout.c
141 * int rewind() c-library
142 * int setjmp() c-library
143 * VOID symglob() assym.c
144 * VOID syminit() assym.c
145 * VOID usage() asmain.c
148 * Completion of main() completes the assembly process.
149 * REL, LST, and/or SYM files may be generated.
160 /*fprintf(stdout, "\n");*/
163 for (i=1; i<argc; ++i) {
169 while ((c = *p++) != 0)
187 case 'j': /* JLH: debug info */
190 ++oflag; /* force object */
237 if (++inpfil == MAXFIL) {
238 fprintf(stderr, "too many input files\n");
241 sfp[inpfil] = afile(p, "", 0);
242 strcpy(srcfn[inpfil],afn);
245 lfp = afile(p, "lst", 1);
247 ofp = afile(p, "rel", 1);
249 tfp = afile(p, "sym", 1);
256 for (pass=0; pass<3; ++pass) {
257 if (gflag && pass == 1)
259 if (aflag && pass == 1)
261 if (oflag && pass == 2)
274 for (i = 0; i <= inpfil; i++)
293 /* JLH: if line begins with ";!", then
294 * pass this comment on to the output file
296 if (oflag && (pass == 1) &&
297 (ip[0] == ';') && (ip[1] == '!'))
299 fprintf(ofp, "%s\n", ip );
302 if (setjmp(jump_env) == 0)
310 newdot(dot.s_area); /* Flush area info */
311 if (flevel || tlevel)
315 outchk(HUGE, HUGE); /* Flush */
326 /*)Function VOID asexit(i)
330 * The function asexit() explicitly closes all open
331 * files and then terminates the program.
337 * FILE * ifp[] array of include-file file handles
338 * FILE * lfp list output file handle
339 * FILE * ofp relocation output file handle
340 * FILE * tfp symbol table output file handle
341 * FILE * sfp[] array of assembler-source file handles
344 * int fclose() c-library
345 * VOID exit() c-library
348 * All files closed. Program terminates.
357 if (lfp != NULL) fclose(lfp);
358 if (ofp != NULL) fclose(ofp);
359 if (tfp != NULL) fclose(tfp);
361 for (j=0; j<MAXFIL && sfp[j] != NULL; j++) {
365 /*for (j=0; j<MAXINC && ifp[j] != NULL; j++) {
372 /*)Function VOID asmbl()
374 * The function asmbl() scans the assembler-source text for
375 * (1) labels, global labels, equates, global equates, and local
376 * symbols, (2) .if, .else, .endif, and .page directives,
377 * (3) machine independent assembler directives, and (4) machine
378 * dependent mnemonics.
381 * mne * mp pointer to a mne structure
382 * sym * sp pointer to a sym structure
383 * tsym * tp pointer to a tsym structure
384 * int c character from assembler-source
386 * area * ap pointer to an area structure
387 * expr e1 expression structure
388 * char id[] id string
389 * char opt[] options string
390 * char fn[] filename string
391 * char * p pointer into a string
392 * int d temporary value
393 * int n temporary value
394 * int uaf user area options flag
395 * int uf area options
398 * area * areap pointer to an area structure
399 * char ctype[] array of character types, one per
401 * int flevel IF-ELSE-ENDIF flag will be non
402 * zero for false conditional case
403 * Addr_T fuzz tracks pass to pass changes in the
404 * address of symbols caused by
405 * variable length instruction formats
406 * int ifcnd[] array of IF statement condition
407 * values (0 = FALSE) indexed by tlevel
408 * int iflvl[] array of IF-ELSE-ENDIF flevel
409 * values indexed by tlevel
410 * FILE * ifp[] array of include-file file handles
411 * char incfn[][] array of include file names
412 * int incline[] current include file line
413 * int incfil current file handle index
415 * Addr_T laddr address of current assembler line
416 * or value of .if argument
417 * int lmode listing mode
418 * int lop current line number on page
419 * char module[] module name string
420 * int pass assembler pass number
421 * int radix current number conversion radix:
422 * 2 (binary), 8 (octal), 10 (decimal),
424 * char stb[] Subtitle string buffer
425 * sym * symp pointer to a symbol structure
426 * char tb[] Title string buffer
427 * int tlevel current conditional level
430 * Addr_T absexpr() asexpr.c
431 * area * alookup() assym.c
432 * VOID clrexpr() asexpr.c
433 * int digit() asexpr.c
434 * char endline() aslex.c
435 * VOID err() assubr.c
436 * VOID expr() asexpr.c
437 * FILE * fopen() c-library
439 * VOID getid() aslex.c
440 * int getmap() aslex.c
441 * char getnb() aslex.c
442 * VOID getst() aslex.c
443 * sym * lookup() assym.c
444 * VOID machin() ___mch.c
445 * mne * mlookup() assym.c
447 * VOID * new() assym.c
448 * VOID newdot() asmain.c
449 * VOID outall() asout.c
450 * VOID outab() asout.c
451 * VOID outchk() asout.c
452 * VOID outrb() asout.c
453 * VOID outrw() asout.c
454 * VOID phase() asmain.c
455 * VOID qerr() assubr.c
456 * char * strcpy() c-library
457 * char * strncpy() c-library
458 * VOID unget() aslex.c
464 register struct mne *mp;
465 register struct sym *sp;
466 register struct tsym *tp;
479 if ((c=endline()) == 0) { return; }
481 * If the first character is a digit then assume
482 * a local symbol is being specified. The symbol
483 * must end with $: to be valid.
485 * Construct a tsym structure at the first
486 * occurance of the symbol. Flag the symbol
487 * as multiply defined if not the first occurance.
489 * Load area, address, and fuzz values
490 * into structure tsym.
492 * Check for assembler phase error and
493 * multiply defined error.
495 if (ctype[c] & DIGIT) {
499 while ((d = digit(c, 10)) >= 0) {
503 if (c != '$' || get() != ':')
508 if (n == tp->t_num) {
515 tp=(struct tsym *) new (sizeof(struct tsym));
516 tp->t_lnk = symp->s_tsym;
519 tp->t_area = dot.s_area;
520 tp->t_addr = dot.s_addr;
525 if (n == tp->t_num) {
532 fuzz = tp->t_addr - dot.s_addr;
533 tp->t_area = dot.s_area;
534 tp->t_addr = dot.s_addr;
536 phase(tp->t_area, tp->t_addr);
537 if (tp->t_flg & S_MDF)
548 * If the first character is a letter then assume a lable,
549 * symbol, assembler directive, or assembler mnemonic is
552 if ((ctype[c] & LETTER) == 0) {
562 * If the next character is a : then a label is being processed.
563 * A double :: defines a global label. If this is new label
564 * then create a symbol structure.
566 * Flag multiply defined labels.
568 * Load area, address, and fuzz values
569 * into structure symp.
571 * Check for assembler phase error and
572 * multiply defined error.
577 if ((c = get()) != ':') {
585 if ((symp->s_type != S_NEW) &&
586 ((symp->s_flag & S_ASG) == 0))
587 symp->s_flag |= S_MDF;
589 fuzz = symp->s_addr - dot.s_addr;
590 symp->s_type = S_USER;
591 symp->s_area = dot.s_area;
592 symp->s_addr = dot.s_addr;
594 if (symp->s_flag & S_MDF)
596 phase(symp->s_area, symp->s_addr);
599 symp->s_flag |= S_GBL;
605 * If the next character is a = then an equate is being processed.
606 * A double == defines a global equate. If this is new variable
607 * then create a symbol structure.
612 if ((c = get()) != '=') {
621 if (e1.e_flag || e1.e_base.e_ap != dot.s_area)
624 if (sp->s_type != S_NEW && (sp->s_flag & S_ASG) == 0) {
628 sp->s_area = e1.e_base.e_ap;
629 sp->s_addr = laddr = e1.e_addr;
638 lmode = flevel ? SLIST : CLIST;
639 if ((mp = mlookup(id)) == NULL) {
645 * If we have gotten this far then we have found an
646 * assembler directive or an assembler mnemonic.
648 * Check for .if, .else, .endif, and .page directives
649 * which are not controlled by the conditional flags
651 switch (mp->m_type) {
655 if (tlevel < MAXIF) {
658 iflvl[tlevel] = flevel;
671 if (++flevel > (iflvl[tlevel]+1)) {
675 if (--flevel < iflvl[tlevel]) {
684 flevel = iflvl[tlevel--];
702 * If we are not in a false state for .if/.else then
703 * process the assembler directives here.
705 switch (mp->m_type) {
709 laddr = dot.s_addr = (dot.s_addr + 1) & ~1;
715 laddr = dot.s_addr |= 1;
724 if (mp->m_type == S_BYTE) {
729 } while ((c = getnb()) == ',');
735 if ((d = getnb()) == '\0')
737 while ((c = getmap(d)) >= 0)
739 if (mp->m_type == S_ASCIZ)
744 if ((d = getnb()) == '\0')
748 if ((n = getmap(d)) >= 0) {
761 dot.s_addr += e1.e_addr*mp->m_valu;
767 if ((c = getnb()) != 0) {
769 if (p < &tb[NTITL-1])
771 } while ((c = get()) != 0);
780 if ((c = getnb()) != 0) {
782 if (p < &stb[NSBTL-1])
784 } while ((c = get()) != 0);
797 strncpy(module, id, NCPS);
808 } while ((c = getnb()) == ',');
817 if ((c = getnb()) == '(') {
821 if (mp && mp->m_type == S_ATYP) {
827 } while ((c = getnb()) == ',');
833 if ((ap = alookup(id)) != NULL) {
834 if (uaf && uf != ap->a_flag)
836 if (ap->a_flag & A_OVR) {
841 ap = (struct area *) new (sizeof(struct area));
843 strncpy(ap->a_id, id, NCPS);
844 ap->a_ref = areap->a_ref + 1;
847 ap->a_flag = uaf ? uf : (A_CON|A_REL);
855 if (dot.s_area->a_flag & A_ABS) {
857 laddr = dot.s_addr = absexpr();
903 while ((c = get()) != d) {
904 if (p < &fn[FILSPC-1]) {
911 if (++incfil == MAXINC ||
912 (ifp[incfil] = fopen(fn, "r")) == NULL) {
918 strcpy(incfn[incfil],fn);
928 if (!strcmpi(id, "on"))
930 /* Quick sanity check: size of
931 * Addr_T must be at least 24 bits.
933 if (sizeof(Addr_T) < 3)
937 "Cannot enable Flat24 mode: "
938 "host system must have 24 bit "
939 "or greater integers.\n");
946 else if (!strcmpi(id, "off"))
961 printf("as8051: ds390 flat mode %sabled.\n",
962 flat24Mode ? "en" : "dis");
968 * If not an assembler directive then go to
969 * the machine dependent function which handles
970 * all the assembler mnemonics.
974 /* if cdb information the generate the line info */
975 if (cflag && (pass == 1))
978 /* JLH: if -j, generate a line number symbol */
979 if (jflag && (pass == 1))
988 /*)Function FILE * afile(fn, ft, wf)
990 * char * fn file specification string
991 * char * ft file type string
992 * int wf read(0)/write(1) flag
994 * The function afile() opens a file for reading or writing.
995 * (1) If the file type specification string ft
996 * is not NULL then a file specification is
997 * constructed with the file path\name in fn
998 * and the extension in ft.
999 * (2) If the file type specification string ft
1000 * is NULL then the file specification is
1001 * constructed from fn. If fn does not have
1002 * a file type then the default source file
1003 * type dsft is appended to the file specification.
1005 * afile() returns a file handle for the opened file or aborts
1006 * the assembler on an open error.
1009 * int c character value
1010 * FILE * fp filehandle for opened file
1011 * char * p1 pointer to filespec string fn
1012 * char * p2 pointer to filespec string fb
1013 * char * p3 pointer to filetype string ft
1016 * char afn[] afile() constructed filespec
1017 * char dsft[] default assembler file type string
1018 * char afn[] constructed file specification string
1021 * VOID asexit() asmain.c
1022 * FILE * fopen() c_library
1023 * int fprintf() c_library
1026 * File is opened for read or write.
1035 register char *p1, *p2, *p3;
1042 while ((c = *p1++) != 0 && c != FSEPX) {
1043 if (p2 < &afn[FILSPC-4])
1054 while ((c = *p3++) != 0) {
1055 if (p2 < &afn[FILSPC-1])
1059 if ((fp = fopen(afn, wf?"w":"r")) == NULL) {
1060 fprintf(stderr, "%s: cannot %s.\n", afn, wf?"create":"open");
1066 /*)Function VOID newdot(nap)
1068 * area * nap pointer to the new area structure
1070 * The function newdot():
1071 * (1) copies the current values of fuzz and the last
1072 * address into the current area referenced by dot
1073 * (2) loads dot with the pointer to the new area and
1074 * loads the fuzz and last address parameters
1075 * (3) outall() is called to flush any remaining
1076 * bufferred code from the old area to the output
1079 * area * oap pointer to old area
1082 * sym dot defined as sym[0]
1083 * Addr_T fuzz tracks pass to pass changes in the
1084 * address of symbols caused by
1085 * variable length instruction formats
1091 * Current area saved, new area loaded, buffers flushed.
1096 register struct area *nap;
1098 register struct area *oap;
1102 oap->a_size = dot.s_addr;
1105 dot.s_addr = nap->a_size;
1109 /*)Function VOID phase(ap, a)
1111 * area * ap pointer to area
1112 * Addr_T a address in area
1114 * Function phase() compares the area ap and address a
1115 * with the current area dot.s_area and address dot.s_addr
1116 * to determine if the position of the symbol has changed
1117 * between assembler passes.
1123 * sym * dot defined as sym[0]
1129 * The p error is invoked if the area and/or address
1138 if (ap != dot.s_area || a != dot.s_addr)
1143 "Usage: [-dqxjgalopsf] file1 [file2 file3 ...]",
1144 " d decimal listing",
1146 " x hex listing (default)",
1147 " j add line number and debug information to file", /* JLH */
1148 " g undefined symbols made global",
1149 " a all user symbols made global",
1150 " l create list output file1[LST]",
1151 " o create object output file1[REL]",
1152 " s create symbol output file1[SYM]",
1153 " p disable listing pagination",
1154 " f flag relocatable references by ` in listing file",
1155 " ff flag relocatable references by mode in listing file",
1160 /*)Function VOID usage()
1162 * The function usage() outputs to the stderr device the
1163 * assembler name and version and a list of valid assembler options.
1166 * char ** dp pointer to an array of
1167 * text string pointers.
1170 * char cpu[] assembler type string
1171 * char * usetxt[] array of string pointers
1174 * VOID asexit() asmain.c
1175 * int fprintf() c_library
1178 * program is terminated
1186 fprintf(stderr, "\nASxxxx Assembler %s (%s)\n\n", VERSION, cpu);
1187 for (dp = usetxt; *dp; dp++)
1188 fprintf(stderr, "%s\n", *dp);