4 * (C) Copyright 1989-1995
11 * 29-Oct-97 JLH pass ";!" comments to output file
18 //#if defined(_MSC_VER)
26 extern VOID machine(struct mne *);
29 * The module asmain.c includes the command argument parser,
30 * the three pass sequencer, and the machine independent
31 * assembler parsing code.
33 * asmain.c contains the following functions:
34 * VOID main(argc, argv)
37 * FILE * afile(fn, ft, wf)
42 * asmain.c contains the array char *usetxt[] which
43 * references the usage text strings printed by usage().
46 /*)Function VOID main(argc, argv)
48 * int argc argument count
49 * char * argv array of pointers to argument strings
51 * The function main() is the entry point to the assembler.
52 * The purpose of main() is to (1) parse the command line
53 * arguments for options and source file specifications and
54 * (2) to process the source files through the 3 pass assembler.
55 * Before each assembler pass various variables are initialized
56 * and source files are rewound to their beginning. During each
57 * assembler pass each assembler-source text line is processed.
58 * After each assembler pass the assembler information is flushed
59 * to any opened output files and the if-else-endif processing
60 * is checked for proper termination.
62 * The function main() is also responsible for opening all
63 * output files (REL, LST, and SYM), sequencing the global (-g)
64 * and all-global (-a) variable definitions, and dumping the
65 * REL file header information.
68 * char * p pointer to argument string
69 * int c character from argument string
70 * int i argument loop counter
71 * area * ap pointer to area structure
74 * int aflag -a, make all symbols global flag
75 * char afn[] afile() constructed filespec
76 * area * areap pointer to an area structure
77 * int cb[] array of assembler output values
78 * int cbt[] array of assembler relocation types
79 * describing the data in cb[]
80 * int cfile current file handle index
81 * of input assembly files
82 * int * cp pointer to assembler output array cb[]
83 * int * cpt pointer to assembler relocation type
85 * char eb[] array of generated error codes
86 * char * ep pointer into error list array eb[]
87 * int fflag -f(f), relocations flagged flag
88 * int flevel IF-ELSE-ENDIF flag will be non
89 * zero for false conditional case
90 * Addr_T fuzz tracks pass to pass changes in the
91 * address of symbols caused by
92 * variable length instruction formats
93 * int gflag -g, make undefined symbols global flag
94 * char ib[] assembler-source text line
95 * int inpfil count of assembler
96 * input files specified
97 * int ifcnd[] array of IF statement condition
98 * values (0 = FALSE) indexed by tlevel
99 * int iflvl[] array of IF-ELSE-ENDIF flevel
100 * values indexed by tlevel
101 * int incfil current file handle index
103 * char * ip pointer into the assembler-source
105 * jmp_buf jump_env compiler dependent structure
106 * used by setjmp() and longjmp()
107 * int lflag -l, generate listing flag
108 * int line current assembler source
110 * int lop current line number on page
111 * int oflag -o, generate relocatable output flag
112 * int jflag -j, generate debug info 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 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.
167 /*fprintf(stdout, "\n");*/
170 for (i=1; i<argc; ++i) {
176 while ((c = *p++) != 0)
194 case 'j': /* JLH: debug info */
197 ++oflag; /* force object */
244 if (++inpfil == MAXFIL) {
245 fprintf(stderr, "too many input files\n");
248 sfp[inpfil] = afile(p, "", 0);
249 strcpy(srcfn[inpfil],afn);
252 lfp = afile(p, "lst", 1);
254 ofp = afile(p, "rel", 1);
255 // save the file name if we have to delete it on error
259 tfp = afile(p, "sym", 1);
266 for (pass=0; pass<3; ++pass) {
267 if (gflag && pass == 1)
269 if (aflag && pass == 1)
271 if (oflag && pass == 2)
284 for (i = 0; i <= inpfil; i++)
303 /* JLH: if line begins with ";!", then
304 * pass this comment on to the output file
306 if (oflag && (pass == 1) &&
307 (ip[0] == ';') && (ip[1] == '!'))
309 fprintf(ofp, "%s\n", ip );
312 if (setjmp(jump_env) == 0)
320 newdot(dot.s_area); /* Flush area info */
321 if (flevel || tlevel)
325 outchk(HUGE, HUGE); /* Flush */
332 //printf ("aserr: %d\n", aserr);
333 //printf ("fatalErrors: %d\n", fatalErrors);
335 return 0; // hush the compiler
338 /*)Function VOID asexit(i)
342 * The function asexit() explicitly closes all open
343 * files and then terminates the program.
349 * FILE * ifp[] array of include-file file handles
350 * FILE * lfp list output file handle
351 * FILE * ofp relocation output file handle
352 * FILE * tfp symbol table output file handle
353 * FILE * sfp[] array of assembler-source file handles
356 * int fclose() c-library
357 * VOID exit() c-library
360 * All files closed. Program terminates.
369 if (lfp != NULL) fclose(lfp);
370 if (ofp != NULL) fclose(ofp);
371 if (tfp != NULL) fclose(tfp);
373 for (j=0; j<MAXFIL && sfp[j] != NULL; j++) {
377 /*for (j=0; j<MAXINC && ifp[j] != NULL; j++) {
381 // remove output file
382 printf ("removing %s\n", relFile);
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 machin() ___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;
495 if ((c=endline()) == 0) { return; }
497 * If the first character is a digit then assume
498 * a local symbol is being specified. The symbol
499 * must end with $: to be valid.
501 * Construct a tsym structure at the first
502 * occurance of the symbol. Flag the symbol
503 * as multiply defined if not the first occurance.
505 * Load area, address, and fuzz values
506 * into structure tsym.
508 * Check for assembler phase error and
509 * multiply defined error.
511 if (ctype[c] & DIGIT) {
515 while ((d = digit(c, 10)) >= 0) {
519 if (c != '$' || get() != ':')
524 if (n == tp->t_num) {
531 tp=(struct tsym *) new (sizeof(struct tsym));
532 tp->t_lnk = symp->s_tsym;
535 tp->t_area = dot.s_area;
536 tp->t_addr = dot.s_addr;
541 if (n == tp->t_num) {
548 fuzz = tp->t_addr - dot.s_addr;
549 tp->t_area = dot.s_area;
550 tp->t_addr = dot.s_addr;
552 phase(tp->t_area, tp->t_addr);
553 if (tp->t_flg & S_MDF)
564 * If the first character is a letter then assume a lable,
565 * symbol, assembler directive, or assembler mnemonic is
568 if ((ctype[c] & LETTER) == 0) {
578 * If the next character is a : then a label is being processed.
579 * A double :: defines a global label. If this is new label
580 * then create a symbol structure.
582 * Flag multiply defined labels.
584 * Load area, address, and fuzz values
585 * into structure symp.
587 * Check for assembler phase error and
588 * multiply defined error.
593 if ((c = get()) != ':') {
601 if ((symp->s_type != S_NEW) &&
602 ((symp->s_flag & S_ASG) == 0))
603 symp->s_flag |= S_MDF;
605 fuzz = symp->s_addr - dot.s_addr;
606 symp->s_type = S_USER;
607 symp->s_area = dot.s_area;
608 symp->s_addr = dot.s_addr;
610 if (symp->s_flag & S_MDF)
612 phase(symp->s_area, symp->s_addr);
615 symp->s_flag |= S_GBL;
621 * If the next character is a = then an equate is being processed.
622 * A double == defines a global equate. If this is new variable
623 * then create a symbol structure.
628 if ((c = get()) != '=') {
637 if (e1.e_flag || e1.e_base.e_ap != dot.s_area)
640 if (sp->s_type != S_NEW && (sp->s_flag & S_ASG) == 0) {
644 sp->s_area = e1.e_base.e_ap;
645 sp->s_addr = laddr = e1.e_addr;
654 lmode = flevel ? SLIST : CLIST;
655 if ((mp = mlookup(id)) == NULL) {
661 * If we have gotten this far then we have found an
662 * assembler directive or an assembler mnemonic.
664 * Check for .if, .else, .endif, and .page directives
665 * which are not controlled by the conditional flags
667 switch (mp->m_type) {
671 if (tlevel < MAXIF) {
674 iflvl[tlevel] = flevel;
687 if (++flevel > (iflvl[tlevel]+1)) {
691 if (--flevel < iflvl[tlevel]) {
700 flevel = iflvl[tlevel--];
718 * If we are not in a false state for .if/.else then
719 * process the assembler directives here.
721 switch (mp->m_type) {
725 laddr = dot.s_addr = (dot.s_addr + 1) & ~1;
731 laddr = dot.s_addr |= 1;
740 if (mp->m_type == S_BYTE) {
745 } while ((c = getnb()) == ',');
751 if ((d = getnb()) == '\0')
753 while ((c = getmap(d)) >= 0)
755 if (mp->m_type == S_ASCIZ)
760 if ((d = getnb()) == '\0')
764 if ((n = getmap(d)) >= 0) {
777 dot.s_addr += e1.e_addr*mp->m_valu;
783 if ((c = getnb()) != 0) {
785 if (p < &tb[NTITL-1])
787 } while ((c = get()) != 0);
796 if ((c = getnb()) != 0) {
798 if (p < &stb[NSBTL-1])
800 } while ((c = get()) != 0);
813 strncpy(module, id, NCPS);
824 } while ((c = getnb()) == ',');
833 if ((c = getnb()) == '(') {
837 if (mp && mp->m_type == S_ATYP) {
843 } while ((c = getnb()) == ',');
849 if ((ap = alookup(id)) != NULL) {
850 if (uaf && uf != ap->a_flag)
852 if (ap->a_flag & A_OVR) {
857 ap = (struct area *) new (sizeof(struct area));
859 strncpy(ap->a_id, id, NCPS);
860 ap->a_ref = areap->a_ref + 1;
863 ap->a_flag = uaf ? uf : (A_CON|A_REL);
871 if (dot.s_area->a_flag & A_ABS) {
873 laddr = dot.s_addr = absexpr();
919 while ((c = get()) != d) {
920 if (p < &fn[FILSPC-1]) {
927 if (++incfil == MAXINC ||
928 (ifp[incfil] = fopen(fn, "r")) == NULL) {
934 strcpy(incfn[incfil],fn);
944 if (!strcmpi(id, "on"))
946 /* Quick sanity check: size of
947 * Addr_T must be at least 24 bits.
949 if (sizeof(Addr_T) < 3)
953 "Cannot enable Flat24 mode: "
954 "host system must have 24 bit "
955 "or greater integers.\n");
962 else if (!strcmpi(id, "off"))
977 printf("as8051: ds390 flat mode %sabled.\n",
978 flat24Mode ? "en" : "dis");
984 * If not an assembler directive then go to
985 * the machine dependent function which handles
986 * all the assembler mnemonics.
990 /* if cdb information the generate the line info */
991 if (cflag && (pass == 1))
994 /* JLH: if -j, generate a line number symbol */
995 if (jflag && (pass == 1))
1004 /*)Function FILE * afile(fn, ft, wf)
1006 * char * fn file specification string
1007 * char * ft file type string
1008 * int wf read(0)/write(1) flag
1010 * The function afile() opens a file for reading or writing.
1011 * (1) If the file type specification string ft
1012 * is not NULL then a file specification is
1013 * constructed with the file path\name in fn
1014 * and the extension in ft.
1015 * (2) If the file type specification string ft
1016 * is NULL then the file specification is
1017 * constructed from fn. If fn does not have
1018 * a file type then the default source file
1019 * type dsft is appended to the file specification.
1021 * afile() returns a file handle for the opened file or aborts
1022 * the assembler on an open error.
1025 * int c character value
1026 * FILE * fp filehandle for opened file
1027 * char * p1 pointer to filespec string fn
1028 * char * p2 pointer to filespec string fb
1029 * char * p3 pointer to filetype string ft
1032 * char afn[] afile() constructed filespec
1033 * char dsft[] default assembler file type string
1034 * char afn[] constructed file specification string
1037 * VOID asexit() asmain.c
1038 * FILE * fopen() c_library
1039 * int fprintf() c_library
1042 * File is opened for read or write.
1051 register char *p1, *p2, *p3;
1058 while ((c = *p1++) != 0 && c != FSEPX) {
1059 if (p2 < &afn[FILSPC-4])
1070 while ((c = *p3++) != 0) {
1071 if (p2 < &afn[FILSPC-1])
1075 if ((fp = fopen(afn, wf?"w":"r")) == NULL) {
1076 fprintf(stderr, "%s: cannot %s.\n", afn, wf?"create":"open");
1082 /*)Function VOID newdot(nap)
1084 * area * nap pointer to the new area structure
1086 * The function newdot():
1087 * (1) copies the current values of fuzz and the last
1088 * address into the current area referenced by dot
1089 * (2) loads dot with the pointer to the new area and
1090 * loads the fuzz and last address parameters
1091 * (3) outall() is called to flush any remaining
1092 * bufferred code from the old area to the output
1095 * area * oap pointer to old area
1098 * sym dot defined as sym[0]
1099 * Addr_T fuzz tracks pass to pass changes in the
1100 * address of symbols caused by
1101 * variable length instruction formats
1107 * Current area saved, new area loaded, buffers flushed.
1112 register struct area *nap;
1114 register struct area *oap;
1118 oap->a_size = dot.s_addr;
1121 dot.s_addr = nap->a_size;
1125 /*)Function VOID phase(ap, a)
1127 * area * ap pointer to area
1128 * Addr_T a address in area
1130 * Function phase() compares the area ap and address a
1131 * with the current area dot.s_area and address dot.s_addr
1132 * to determine if the position of the symbol has changed
1133 * between assembler passes.
1139 * sym * dot defined as sym[0]
1145 * The p error is invoked if the area and/or address
1154 if (ap != dot.s_area || a != dot.s_addr)
1159 "Usage: [-dqxjgalopsf] file1 [file2 file3 ...]",
1160 " d decimal listing",
1162 " x hex listing (default)",
1163 " j add line number and debug information to file", /* JLH */
1164 " g undefined symbols made global",
1165 " a all user symbols made global",
1166 " l create list output file1[LST]",
1167 " o create object output file1[REL]",
1168 " s create symbol output file1[SYM]",
1169 " p disable listing pagination",
1170 " f flag relocatable references by ` in listing file",
1171 " ff flag relocatable references by mode in listing file",
1176 /*)Function VOID usage()
1178 * The function usage() outputs to the stderr device the
1179 * assembler name and version and a list of valid assembler options.
1182 * char ** dp pointer to an array of
1183 * text string pointers.
1186 * char cpu[] assembler type string
1187 * char * usetxt[] array of string pointers
1190 * VOID asexit() asmain.c
1191 * int fprintf() c_library
1194 * program is terminated
1202 fprintf(stderr, "\nASxxxx Assembler %s (%s)\n\n", VERSION, cpu);
1203 for (dp = usetxt; *dp; dp++)
1204 fprintf(stderr, "%s\n", *dp);