4 * (C) Copyright 1989-1995
13 * Extensions: P. Felber
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 page current page number
113 * int pflag enable listing pagination
114 * int pass assembler pass number
115 * int radix current number conversion radix:
116 * 2 (binary), 8 (octal), 10 (decimal),
118 * int sflag -s, generate symbol table flag
119 * char srcfn[][] array of source file names
120 * int srcline[] current source file line
121 * char stb[] Subtitle string buffer
122 * sym * symp pointer to a symbol structure
123 * int tlevel current conditional level
124 * int xflag -x, listing radix flag
125 * FILE * lfp list output file handle
126 * FILE * ofp relocation output file handle
127 * FILE * tfp symbol table output file handle
128 * FILE * sfp[] array of assembler-source file handles
131 * FILE * afile() asmain.c
132 * VOID allglob() assym.c
133 * VOID asexit() asmain.c
134 * VOID diag() assubr.c
135 * VOID err() assubr.c
136 * int fprintf() c-library
137 * int getline() aslex.c
138 * VOID list() aslist.c
139 * VOID lstsym() aslist.c
140 * VOID minit() ___mch.c
141 * VOID newdot() asmain.c
142 * VOID outchk() asout.c
143 * VOID outgsd() asout.c
144 * int rewind() c-library
145 * int setjmp() c-library
146 * VOID symglob() assym.c
147 * VOID syminit() assym.c
148 * VOID usage() asmain.c
151 * Completion of main() completes the assembly process.
152 * REL, LST, and/or SYM files may be generated.
156 main(int argc, char **argv)
165 fprintf(stdout, "\n");
169 for (i=1; i<argc; ++i) {
175 while ((c = *p++) != 0)
235 if (++inpfil == MAXFIL) {
236 fprintf(stderr, "too many input files\n");
239 sfp[inpfil] = afile(p, "", 0);
240 strcpy(srcfn[inpfil],afn);
246 lfp = afile(p, "lst", 1);
248 ofp = afile(p, "", 1);
250 tfp = afile(p, "sym", 1);
255 lfp = afile(p, "LST", 1);
257 ofp = afile(p, "REL", 1);
259 tfp = afile(p, "SYM", 1);
267 for (pass=0; pass<3; ++pass) {
268 if (gflag && pass == 1)
270 if (aflag && pass == 1)
272 if (oflag && pass == 2)
285 for (i = 0; i <= inpfil; i++)
303 if (setjmp(jump_env) == 0)
310 newdot(dot.s_area); /* Flush area info */
311 if (flevel || tlevel)
315 outchk(HUGE, HUGE); /* Flush */
325 /*)Function VOID asexit(i)
329 * The function asexit() explicitly closes all open
330 * files and then terminates the program.
336 * FILE * ifp[] array of include-file file handles
337 * FILE * lfp list output file handle
338 * FILE * ofp relocation output file handle
339 * FILE * tfp symbol table output file handle
340 * FILE * sfp[] array of assembler-source file handles
343 * int fclose() c-library
344 * VOID exit() c-library
347 * All files closed. Program terminates.
356 if (lfp != NULL) fclose(lfp);
357 if (ofp != NULL) fclose(ofp);
358 if (tfp != NULL) fclose(tfp);
360 for (j=0; j<MAXFIL && sfp[j] != NULL; j++) {
364 for (j=0; j<MAXINC && ifp[j] != NULL; j++) {
371 /*)Function VOID asmbl()
373 * The function asmbl() scans the assembler-source text for
374 * (1) labels, global labels, equates, global equates, and local
375 * symbols, (2) .if, .else, .endif, and .page directives,
376 * (3) machine independent assembler directives, and (4) machine
377 * dependent mnemonics.
380 * mne * mp pointer to a mne structure
381 * sym * sp pointer to a sym structure
382 * tsym * tp pointer to a tsym structure
383 * int c character from assembler-source
385 * area * ap pointer to an area structure
386 * expr e1 expression structure
387 * char id[] id string
388 * char opt[] options string
389 * char fn[] filename string
390 * char * p pointer into a string
391 * int d temporary value
392 * int n temporary value
393 * int uaf user area options flag
394 * int uf area options
397 * area * areap pointer to an area structure
398 * char ctype[] array of character types, one per
400 * int flevel IF-ELSE-ENDIF flag will be non
401 * zero for false conditional case
402 * addr_t fuzz tracks pass to pass changes in the
403 * address of symbols caused by
404 * variable length instruction formats
405 * int ifcnd[] array of IF statement condition
406 * values (0 = FALSE) indexed by tlevel
407 * int iflvl[] array of IF-ELSE-ENDIF flevel
408 * values indexed by tlevel
409 * FILE * ifp[] array of include-file file handles
410 * char incfn[][] array of include file names
411 * int incline[] current include file line
412 * int incfil current file handle index
414 * addr_t laddr address of current assembler line
415 * or value of .if argument
416 * int lmode listing mode
417 * int lop current line number on page
418 * char module[] module name string
419 * int pass assembler pass number
420 * int radix current number conversion radix:
421 * 2 (binary), 8 (octal), 10 (decimal),
423 * char stb[] Subtitle string buffer
424 * sym * symp pointer to a symbol structure
425 * char tb[] Title string buffer
426 * int tlevel current conditional level
429 * addr_t absexpr() asexpr.c
430 * area * alookup() assym.c
431 * VOID clrexpr() asexpr.c
432 * int digit() asexpr.c
433 * char endline() aslex.c
434 * VOID err() assubr.c
435 * VOID expr() asexpr.c
436 * FILE * fopen() c-library
438 * VOID getid() aslex.c
439 * int getmap() aslex.c
440 * char getnb() aslex.c
441 * VOID getst() aslex.c
442 * sym * lookup() assym.c
443 * VOID machin() ___mch.c
444 * mne * mlookup() assym.c
446 * VOID * new() assym.c
447 * VOID newdot() asmain.c
448 * VOID outall() asout.c
449 * VOID outab() asout.c
450 * VOID outchk() asout.c
451 * VOID outrb() asout.c
452 * VOID outrw() asout.c
453 * VOID phase() asmain.c
454 * VOID qerr() assubr.c
455 * char * strcpy() c-library
456 * char * strncpy() c-library
457 * VOID unget() aslex.c
463 register struct mne *mp;
464 register struct sym *sp;
465 register struct tsym *tp;
477 unsigned int mantissa, exponent;
478 const signed char readbuffer[80];
483 if ((c=endline()) == 0) { return; }
485 * If the first character is a digit then assume
486 * a local symbol is being specified. The symbol
487 * must end with $: to be valid.
489 * Construct a tsym structure at the first
490 * occurance of the symbol. Flag the symbol
491 * as multiply defined if not the first occurance.
493 * Load area, address, and fuzz values
494 * into structure tsym.
496 * Check for assembler phase error and
497 * multiply defined error.
499 if (ctype[c] & DIGIT) {
503 while ((d = digit(c, 10)) >= 0) {
507 if (c != '$' || get() != ':')
512 if (n == tp->t_num) {
519 tp=(struct tsym *) new (sizeof(struct tsym));
520 tp->t_lnk = symp->s_tsym;
523 tp->t_area = dot.s_area;
524 tp->t_addr = dot.s_addr;
529 if (n == tp->t_num) {
536 fuzz = tp->t_addr - dot.s_addr;
537 tp->t_area = dot.s_area;
538 tp->t_addr = dot.s_addr;
540 phase(tp->t_area, tp->t_addr);
541 if (tp->t_flg & S_MDF)
552 * If the first character is a letter then assume a lable,
553 * symbol, assembler directive, or assembler mnemonic is
556 if ((ctype[c] & LETTER) == 0)
565 * If the next character is a : then a label is being processed.
566 * A double :: defines a global label. If this is new label
567 * then create a symbol structure.
569 * Flag multiply defined labels.
571 * Load area, address, and fuzz values
572 * into structure symp.
574 * Check for assembler phase error and
575 * multiply defined error.
580 if ((c = get()) != ':') {
588 if ((symp->s_type != S_NEW) &&
589 ((symp->s_flag & S_ASG) == 0))
590 symp->s_flag |= S_MDF;
592 fuzz = symp->s_addr - dot.s_addr;
593 symp->s_type = S_USER;
594 symp->s_area = dot.s_area;
595 symp->s_addr = dot.s_addr;
597 if (symp->s_flag & S_MDF)
599 phase(symp->s_area, symp->s_addr);
602 symp->s_flag |= S_GBL;
608 * If the next character is a = then an equate is being processed.
609 * A double == defines a global equate. If this is new variable
610 * then create a symbol structure.
615 if ((c = get()) != '=') {
624 if (e1.e_flag || e1.e_base.e_ap != dot.s_area)
627 if (sp->s_type != S_NEW && (sp->s_flag & S_ASG) == 0) {
631 sp->s_area = e1.e_base.e_ap;
632 sp->s_addr = laddr = e1.e_addr;
641 lmode = flevel ? SLIST : CLIST;
642 if ((mp = mlookup(id)) == NULL) {
648 * If we have gotten this far then we have found an
649 * assembler directive or an assembler mnemonic.
651 * Check for .if, .else, .endif, and .page directives
652 * which are not controlled by the conditional flags
654 switch (mp->m_type) {
658 if (tlevel < MAXIF) {
661 iflvl[tlevel] = flevel;
674 if (++flevel > (iflvl[tlevel]+1)) {
678 if (--flevel < iflvl[tlevel]) {
687 flevel = iflvl[tlevel--];
705 * If we are not in a false state for .if/.else then
706 * process the assembler directives here.
708 switch (mp->m_type) {
712 laddr = dot.s_addr = (dot.s_addr + 1) & ~1;
718 laddr = dot.s_addr |= 1;
727 if (mp->m_type == S_BYTE) {
732 } while ((c = getnb()) == ',');
739 getid( readbuffer, ' ' ); /* Hack :) */
740 if ((c=getnb())=='.')
742 getid(&readbuffer[strlen(readbuffer)],'.');
747 f1 = strtod( readbuffer, (char **)NULL );
748 /* Convert f1 to a gb-lib type fp
749 * 24 bit mantissa followed by 7 bit exp and 1 bit sign
755 f2 = floor(log(fabs(f1))/log(2))+1;
756 mantissa = (0x1000000*fabs(f1))/exp(f2*log(2));
758 exponent = f2 + 0x40;
769 outab(mantissa&0xff);
770 outab((mantissa>>8)&0xff);
771 outab((mantissa>>16)&0xff);
772 outab(exponent&0xff);
774 } while ((c = getnb()) == ',');
781 if ((d = getnb()) == '\0')
783 while ((c = getmap(d)) >= 0)
785 if (mp->m_type == S_ASCIZ)
790 if ((d = getnb()) == '\0')
794 if ((n = getmap(d)) >= 0) {
806 dot.s_addr += e1.e_addr*mp->m_valu;
813 if ((c = getnb()) != 0) {
815 if (p < &tb[NTITL-1])
817 } while ((c = get()) != 0);
826 if ((c = getnb()) != 0) {
828 if (p < &stb[NSBTL-1])
830 } while ((c = get()) != 0);
843 strncpy(module, id, NCPS);
854 } while ((c = getnb()) == ',');
863 if ((c = getnb()) == '(') {
867 if (mp && mp->m_type == S_ATYP) {
873 } while ((c = getnb()) == ',');
879 if ((ap = alookup(id)) != NULL) {
880 if (uaf && uf != ap->a_flag)
883 ap = (struct area *) new (sizeof(struct area));
885 strncpy(ap->a_id, id, NCPS);
886 ap->a_ref = areap->a_ref + 1;
889 ap->a_flag = uaf ? uf : (A_CON|A_REL);
897 if (dot.s_area->a_flag & A_ABS) {
899 laddr = dot.s_addr = absexpr();
945 while ((c = get()) != d) {
946 if (p < &fn[FILSPC-1]) {
953 if (++incfil == MAXINC ||
954 (ifp[incfil] = fopen(fn, "r")) == NULL) {
960 strcpy(incfn[incfil],fn);
966 * If not an assembler directive then go to
967 * the machine dependent function which handles
968 * all the assembler mnemonics.
976 /*)Function FILE * afile(fn, ft, wf)
978 * char * fn file specification string
979 * char * ft file type string
980 * int wf read(0)/write(1) flag
982 * The function afile() opens a file for reading or writing.
983 * (1) If the file type specification string ft
984 * is not NULL then a file specification is
985 * constructed with the file path\name in fn
986 * and the extension in ft.
987 * (2) If the file type specification string ft
988 * is NULL then the file specification is
989 * constructed from fn. If fn does not have
990 * a file type then the default source file
991 * type dsft is appended to the file specification.
993 * afile() returns a file handle for the opened file or aborts
994 * the assembler on an open error.
997 * int c character value
998 * FILE * fp filehandle for opened file
999 * char * p1 pointer to filespec string fn
1000 * char * p2 pointer to filespec string fb
1001 * char * p3 pointer to filetype string ft
1004 * char afn[] afile() constructed filespec
1005 * char dsft[] default assembler file type string
1006 * char afn[] constructed file specification string
1009 * VOID asexit() asmain.c
1010 * FILE * fopen() c_library
1011 * int fprintf() c_library
1014 * File is opened for read or write.
1023 register char *p1, *p2, *p3;
1030 while ((c = *p1++) != 0 && c != FSEPX) {
1031 if (p2 < &afn[FILSPC-4])
1042 while ((c = *p3++) != 0) {
1043 if (p2 < &afn[FILSPC-1])
1047 if ((fp = fopen(afn, wf?"w":"r")) == NULL) {
1048 fprintf(stderr, "%s: cannot %s.\n", afn, wf?"create":"open");
1054 /*)Function VOID newdot(nap)
1056 * area * nap pointer to the new area structure
1058 * The function newdot():
1059 * (1) copies the current values of fuzz and the last
1060 * address into the current area referenced by dot
1061 * (2) loads dot with the pointer to the new area and
1062 * loads the fuzz and last address parameters
1063 * (3) outall() is called to flush any remaining
1064 * bufferred code from the old area to the output
1067 * area * oap pointer to old area
1070 * sym dot defined as sym[0]
1071 * addr_t fuzz tracks pass to pass changes in the
1072 * address of symbols caused by
1073 * variable length instruction formats
1079 * Current area saved, new area loaded, buffers flushed.
1084 register struct area *nap;
1086 register struct area *oap;
1090 oap->a_size = dot.s_addr;
1093 dot.s_addr = nap->a_size;
1097 /*)Function VOID phase(ap, a)
1099 * area * ap pointer to area
1100 * addr_t a address in area
1102 * Function phase() compares the area ap and address a
1103 * with the current area dot.s_area and address dot.s_addr
1104 * to determine if the position of the symbol has changed
1105 * between assembler passes.
1111 * sym * dot defined as sym[0]
1117 * The p error is invoked if the area and/or address
1126 if (ap != dot.s_area || a != dot.s_addr)
1132 "Usage: [-dqxgalopsf] outfile file1 [file2 file3 ...]",
1134 "Usage: [-dqxgalopsf] file1 [file2 file3 ...]",
1136 " d decimal listing",
1138 " x hex listing (default)",
1139 " g undefined symbols made global",
1140 " a all user symbols made global",
1142 " l create list output outfile[LST]",
1143 " o create object output outfile[o]",
1144 " s create symbol output outfile[SYM]",
1146 " l create list output file1[LST]",
1147 " o create object output file1[REL]",
1148 " s create symbol output file1[SYM]",
1150 " p disable listing pagination",
1151 " f flag relocatable references by ` in listing file",
1152 " ff flag relocatable references by mode in listing file",
1157 /*)Function VOID usage()
1159 * The function usage() outputs to the stderr device the
1160 * assembler name and version and a list of valid assembler options.
1163 * char ** dp pointer to an array of
1164 * text string pointers.
1167 * char cpu[] assembler type string
1168 * char * usetxt[] array of string pointers
1171 * VOID asexit() asmain.c
1172 * int fprintf() c_library
1175 * program is terminated
1183 fprintf(stderr, "\nASxxxx Assembler %s (%s)\n\n", VERSION, cpu);
1184 for (dp = usetxt; *dp; dp++)
1185 fprintf(stderr, "%s\n", *dp);