4 * (C) Copyright 1989-1995
11 * 29-Oct-97 JLH pass ";!" comments to output file
20 extern VOID machine(struct mne *);
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.
158 /*fprintf(stdout, "\n");*/
161 for (i=1; i<argc; ++i) {
167 while ((c = *p++) != 0)
185 case 'j': /* JLH: debug info */
188 ++oflag; /* force object */
235 if (++inpfil == MAXFIL) {
236 fprintf(stderr, "too many input files\n");
239 sfp[inpfil] = afile(p, "", 0);
240 strcpy(srcfn[inpfil],afn);
243 lfp = afile(p, "lst", 1);
245 ofp = afile(p, "rel", 1);
247 tfp = afile(p, "sym", 1);
254 for (pass=0; pass<3; ++pass) {
255 if (gflag && pass == 1)
257 if (aflag && pass == 1)
259 if (oflag && pass == 2)
272 for (i = 0; i <= inpfil; i++)
291 /* JLH: if line begins with ";!", then
292 * pass this comment on to the output file
294 if (oflag && (pass == 1) &&
295 (ip[0] == ';') && (ip[1] == '!'))
297 fprintf(ofp, "%s\n", ip );
300 if (setjmp(jump_env) == 0)
308 newdot(dot.s_area); /* Flush area info */
309 if (flevel || tlevel)
313 outchk(HUGE, HUGE); /* Flush */
324 /*)Function VOID asexit(i)
328 * The function asexit() explicitly closes all open
329 * files and then terminates the program.
335 * FILE * ifp[] array of include-file file handles
336 * FILE * lfp list output file handle
337 * FILE * ofp relocation output file handle
338 * FILE * tfp symbol table output file handle
339 * FILE * sfp[] array of assembler-source file handles
342 * int fclose() c-library
343 * VOID exit() c-library
346 * All files closed. Program terminates.
355 if (lfp != NULL) fclose(lfp);
356 if (ofp != NULL) fclose(ofp);
357 if (tfp != NULL) fclose(tfp);
359 for (j=0; j<MAXFIL && sfp[j] != NULL; j++) {
363 /*for (j=0; j<MAXINC && ifp[j] != NULL; j++) {
370 /*)Function VOID asmbl()
372 * The function asmbl() scans the assembler-source text for
373 * (1) labels, global labels, equates, global equates, and local
374 * symbols, (2) .if, .else, .endif, and .page directives,
375 * (3) machine independent assembler directives, and (4) machine
376 * dependent mnemonics.
379 * mne * mp pointer to a mne structure
380 * sym * sp pointer to a sym structure
381 * tsym * tp pointer to a tsym structure
382 * int c character from assembler-source
384 * area * ap pointer to an area structure
385 * expr e1 expression structure
386 * char id[] id string
387 * char opt[] options string
388 * char fn[] filename string
389 * char * p pointer into a string
390 * int d temporary value
391 * int n temporary value
392 * int uaf user area options flag
393 * int uf area options
396 * area * areap pointer to an area structure
397 * char ctype[] array of character types, one per
399 * int flevel IF-ELSE-ENDIF flag will be non
400 * zero for false conditional case
401 * addr_t fuzz tracks pass to pass changes in the
402 * address of symbols caused by
403 * variable length instruction formats
404 * int ifcnd[] array of IF statement condition
405 * values (0 = FALSE) indexed by tlevel
406 * int iflvl[] array of IF-ELSE-ENDIF flevel
407 * values indexed by tlevel
408 * FILE * ifp[] array of include-file file handles
409 * char incfn[][] array of include file names
410 * int incline[] current include file line
411 * int incfil current file handle index
413 * addr_t laddr address of current assembler line
414 * or value of .if argument
415 * int lmode listing mode
416 * int lop current line number on page
417 * char module[] module name string
418 * int pass assembler pass number
419 * int radix current number conversion radix:
420 * 2 (binary), 8 (octal), 10 (decimal),
422 * char stb[] Subtitle string buffer
423 * sym * symp pointer to a symbol structure
424 * char tb[] Title string buffer
425 * int tlevel current conditional level
428 * addr_t absexpr() asexpr.c
429 * area * alookup() assym.c
430 * VOID clrexpr() asexpr.c
431 * int digit() asexpr.c
432 * char endline() aslex.c
433 * VOID err() assubr.c
434 * VOID expr() asexpr.c
435 * FILE * fopen() c-library
437 * VOID getid() aslex.c
438 * int getmap() aslex.c
439 * char getnb() aslex.c
440 * VOID getst() aslex.c
441 * sym * lookup() assym.c
442 * VOID machin() ___mch.c
443 * mne * mlookup() assym.c
445 * VOID * new() assym.c
446 * VOID newdot() asmain.c
447 * VOID outall() asout.c
448 * VOID outab() asout.c
449 * VOID outchk() asout.c
450 * VOID outrb() asout.c
451 * VOID outrw() asout.c
452 * VOID phase() asmain.c
453 * VOID qerr() assubr.c
454 * char * strcpy() c-library
455 * char * strncpy() c-library
456 * VOID unget() aslex.c
462 register struct mne *mp;
463 register struct sym *sp;
464 register struct tsym *tp;
477 if ((c=endline()) == 0) { return; }
479 * If the first character is a digit then assume
480 * a local symbol is being specified. The symbol
481 * must end with $: to be valid.
483 * Construct a tsym structure at the first
484 * occurance of the symbol. Flag the symbol
485 * as multiply defined if not the first occurance.
487 * Load area, address, and fuzz values
488 * into structure tsym.
490 * Check for assembler phase error and
491 * multiply defined error.
493 if (ctype[c] & DIGIT) {
497 while ((d = digit(c, 10)) >= 0) {
501 if (c != '$' || get() != ':')
506 if (n == tp->t_num) {
513 tp=(struct tsym *) new (sizeof(struct tsym));
514 tp->t_lnk = symp->s_tsym;
517 tp->t_area = dot.s_area;
518 tp->t_addr = dot.s_addr;
523 if (n == tp->t_num) {
530 fuzz = tp->t_addr - dot.s_addr;
531 tp->t_area = dot.s_area;
532 tp->t_addr = dot.s_addr;
534 phase(tp->t_area, tp->t_addr);
535 if (tp->t_flg & S_MDF)
546 * If the first character is a letter then assume a lable,
547 * symbol, assembler directive, or assembler mnemonic is
550 if ((ctype[c] & LETTER) == 0) {
560 * If the next character is a : then a label is being processed.
561 * A double :: defines a global label. If this is new label
562 * then create a symbol structure.
564 * Flag multiply defined labels.
566 * Load area, address, and fuzz values
567 * into structure symp.
569 * Check for assembler phase error and
570 * multiply defined error.
575 if ((c = get()) != ':') {
583 if ((symp->s_type != S_NEW) &&
584 ((symp->s_flag & S_ASG) == 0))
585 symp->s_flag |= S_MDF;
587 fuzz = symp->s_addr - dot.s_addr;
588 symp->s_type = S_USER;
589 symp->s_area = dot.s_area;
590 symp->s_addr = dot.s_addr;
592 if (symp->s_flag & S_MDF)
594 phase(symp->s_area, symp->s_addr);
597 symp->s_flag |= S_GBL;
603 * If the next character is a = then an equate is being processed.
604 * A double == defines a global equate. If this is new variable
605 * then create a symbol structure.
610 if ((c = get()) != '=') {
619 if (e1.e_flag || e1.e_base.e_ap != dot.s_area)
622 if (sp->s_type != S_NEW && (sp->s_flag & S_ASG) == 0) {
626 sp->s_area = e1.e_base.e_ap;
627 sp->s_addr = laddr = e1.e_addr;
636 lmode = flevel ? SLIST : CLIST;
637 if ((mp = mlookup(id)) == NULL) {
643 * If we have gotten this far then we have found an
644 * assembler directive or an assembler mnemonic.
646 * Check for .if, .else, .endif, and .page directives
647 * which are not controlled by the conditional flags
649 switch (mp->m_type) {
653 if (tlevel < MAXIF) {
656 iflvl[tlevel] = flevel;
669 if (++flevel > (iflvl[tlevel]+1)) {
673 if (--flevel < iflvl[tlevel]) {
682 flevel = iflvl[tlevel--];
700 * If we are not in a false state for .if/.else then
701 * process the assembler directives here.
703 switch (mp->m_type) {
707 laddr = dot.s_addr = (dot.s_addr + 1) & ~1;
713 laddr = dot.s_addr |= 1;
722 if (mp->m_type == S_BYTE) {
727 } while ((c = getnb()) == ',');
733 if ((d = getnb()) == '\0')
735 while ((c = getmap(d)) >= 0)
737 if (mp->m_type == S_ASCIZ)
742 if ((d = getnb()) == '\0')
746 if ((n = getmap(d)) >= 0) {
759 dot.s_addr += e1.e_addr*mp->m_valu;
765 if ((c = getnb()) != 0) {
767 if (p < &tb[NTITL-1])
769 } while ((c = get()) != 0);
778 if ((c = getnb()) != 0) {
780 if (p < &stb[NSBTL-1])
782 } while ((c = get()) != 0);
795 strncpy(module, id, NCPS);
806 } while ((c = getnb()) == ',');
815 if ((c = getnb()) == '(') {
819 if (mp && mp->m_type == S_ATYP) {
825 } while ((c = getnb()) == ',');
831 if ((ap = alookup(id)) != NULL) {
832 if (uaf && uf != ap->a_flag)
834 if (ap->a_flag & A_OVR) {
839 ap = (struct area *) new (sizeof(struct area));
841 strncpy(ap->a_id, id, NCPS);
842 ap->a_ref = areap->a_ref + 1;
845 ap->a_flag = uaf ? uf : (A_CON|A_REL);
853 if (dot.s_area->a_flag & A_ABS) {
855 laddr = dot.s_addr = absexpr();
901 while ((c = get()) != d) {
902 if (p < &fn[FILSPC-1]) {
909 if (++incfil == MAXINC ||
910 (ifp[incfil] = fopen(fn, "r")) == NULL) {
916 strcpy(incfn[incfil],fn);
922 * If not an assembler directive then go to
923 * the machine dependent function which handles
924 * all the assembler mnemonics.
928 /* if cdb information the generate the line info */
929 if (cflag && (pass == 1))
932 /* JLH: if -j, generate a line number symbol */
933 if (jflag && (pass == 1))
942 /*)Function FILE * afile(fn, ft, wf)
944 * char * fn file specification string
945 * char * ft file type string
946 * int wf read(0)/write(1) flag
948 * The function afile() opens a file for reading or writing.
949 * (1) If the file type specification string ft
950 * is not NULL then a file specification is
951 * constructed with the file path\name in fn
952 * and the extension in ft.
953 * (2) If the file type specification string ft
954 * is NULL then the file specification is
955 * constructed from fn. If fn does not have
956 * a file type then the default source file
957 * type dsft is appended to the file specification.
959 * afile() returns a file handle for the opened file or aborts
960 * the assembler on an open error.
963 * int c character value
964 * FILE * fp filehandle for opened file
965 * char * p1 pointer to filespec string fn
966 * char * p2 pointer to filespec string fb
967 * char * p3 pointer to filetype string ft
970 * char afn[] afile() constructed filespec
971 * char dsft[] default assembler file type string
972 * char afn[] constructed file specification string
975 * VOID asexit() asmain.c
976 * FILE * fopen() c_library
977 * int fprintf() c_library
980 * File is opened for read or write.
989 register char *p1, *p2, *p3;
996 while ((c = *p1++) != 0 && c != FSEPX) {
997 if (p2 < &afn[FILSPC-4])
1008 while ((c = *p3++) != 0) {
1009 if (p2 < &afn[FILSPC-1])
1013 if ((fp = fopen(afn, wf?"w":"r")) == NULL) {
1014 fprintf(stderr, "%s: cannot %s.\n", afn, wf?"create":"open");
1020 /*)Function VOID newdot(nap)
1022 * area * nap pointer to the new area structure
1024 * The function newdot():
1025 * (1) copies the current values of fuzz and the last
1026 * address into the current area referenced by dot
1027 * (2) loads dot with the pointer to the new area and
1028 * loads the fuzz and last address parameters
1029 * (3) outall() is called to flush any remaining
1030 * bufferred code from the old area to the output
1033 * area * oap pointer to old area
1036 * sym dot defined as sym[0]
1037 * addr_t fuzz tracks pass to pass changes in the
1038 * address of symbols caused by
1039 * variable length instruction formats
1045 * Current area saved, new area loaded, buffers flushed.
1050 register struct area *nap;
1052 register struct area *oap;
1056 oap->a_size = dot.s_addr;
1059 dot.s_addr = nap->a_size;
1063 /*)Function VOID phase(ap, a)
1065 * area * ap pointer to area
1066 * addr_t a address in area
1068 * Function phase() compares the area ap and address a
1069 * with the current area dot.s_area and address dot.s_addr
1070 * to determine if the position of the symbol has changed
1071 * between assembler passes.
1077 * sym * dot defined as sym[0]
1083 * The p error is invoked if the area and/or address
1092 if (ap != dot.s_area || a != dot.s_addr)
1097 "Usage: [-dqxjgalopsf] file1 [file2 file3 ...]",
1098 " d decimal listing",
1100 " x hex listing (default)",
1101 " j add line number and debug information to file", /* JLH */
1102 " g undefined symbols made global",
1103 " a all user symbols made global",
1104 " l create list output file1[LST]",
1105 " o create object output file1[REL]",
1106 " s create symbol output file1[SYM]",
1107 " p disable listing pagination",
1108 " f flag relocatable references by ` in listing file",
1109 " ff flag relocatable references by mode in listing file",
1114 /*)Function VOID usage()
1116 * The function usage() outputs to the stderr device the
1117 * assembler name and version and a list of valid assembler options.
1120 * char ** dp pointer to an array of
1121 * text string pointers.
1124 * char cpu[] assembler type string
1125 * char * usetxt[] array of string pointers
1128 * VOID asexit() asmain.c
1129 * int fprintf() c_library
1132 * program is terminated
1140 fprintf(stderr, "\nASxxxx Assembler %s (%s)\n\n", VERSION, cpu);
1141 for (dp = usetxt; *dp; dp++)
1142 fprintf(stderr, "%s\n", *dp);