4 * (C) Copyright 1989-1995
11 * 29-Oct-97 JLH pass ";!" comments to output file
24 * The module asmain.c includes the command argument parser,
25 * the three pass sequencer, and the machine independent
26 * assembler parsing code.
28 * asmain.c contains the following functions:
29 * VOID main(argc, argv)
32 * FILE * afile(fn, ft, wf)
37 * asmain.c contains the array char *usetxt[] which
38 * references the usage text strings printed by usage().
41 static const char *search_path[100];
42 static int search_path_length;
45 * The search_path_append is used to append another directory to the end
46 * of the include file search path.
49 * The directory to be added to the path.
52 search_path_append(const char *dir)
54 if (search_path_length < sizeof(search_path)/sizeof(char*))
56 search_path[search_path_length++] = dir;
61 * The search_path_fopen function is used to open the named file. If
62 * the file isn't in the current directory, the search path is then used
63 * to build a series of possible file names, and attempts to open them.
64 * The first found is used.
67 * The name of the file to be opened.
69 * The mode of the file to be opened.
71 * what the fopen function would return on success, or NULL if the
72 * file is not anywhere in the search path.
75 search_path_fopen(const char *filename, const char *mode)
80 fp = fopen(filename, mode);
81 if (fp != NULL || filename[0] == '/' || filename[0] == '\\')
83 for (j = 0; j < search_path_length; ++j)
87 strncpy(path, search_path[j], sizeof(path));
88 if ((path[strlen(path) - 1] != '/') &&
89 (path[strlen(path) - 1] != DIR_SEPARATOR_CHAR))
91 strncat(path, DIR_SEPARATOR_STRING, sizeof(path));
93 strncat(path, filename, sizeof(path));
94 fp = fopen(path, mode);
102 /*)Function VOID main(argc, argv)
104 * int argc argument count
105 * char * argv array of pointers to argument strings
107 * The function main() is the entry point to the assembler.
108 * The purpose of main() is to (1) parse the command line
109 * arguments for options and source file specifications and
110 * (2) to process the source files through the 3 pass assembler.
111 * Before each assembler pass various variables are initialized
112 * and source files are rewound to their beginning. During each
113 * assembler pass each assembler-source text line is processed.
114 * After each assembler pass the assembler information is flushed
115 * to any opened output files and the if-else-endif processing
116 * is checked for proper termination.
118 * The function main() is also responsible for opening all
119 * output files (REL, LST, and SYM), sequencing the global (-g)
120 * and all-global (-a) variable definitions, and dumping the
121 * REL file header information.
124 * char * p pointer to argument string
125 * int c character from argument string
126 * int i argument loop counter
127 * area * ap pointer to area structure
130 * int aflag -a, make all symbols global flag
131 * char afn[] afile() constructed filespec
132 * area * areap pointer to an area structure
133 * int cb[] array of assembler output values
134 * int cbt[] array of assembler relocation types
135 * describing the data in cb[]
136 * int cfile current file handle index
137 * of input assembly files
138 * int * cp pointer to assembler output array cb[]
139 * int * cpt pointer to assembler relocation type
141 * char eb[] array of generated error codes
142 * char * ep pointer into error list array eb[]
143 * int fflag -f(f), relocations flagged flag
144 * int flevel IF-ELSE-ENDIF flag will be non
145 * zero for false conditional case
146 * Addr_T fuzz tracks pass to pass changes in the
147 * address of symbols caused by
148 * variable length instruction formats
149 * int gflag -g, make undefined symbols global flag
150 * char ib[] assembler-source text line
151 * int inpfil count of assembler
152 * input files specified
153 * int ifcnd[] array of IF statement condition
154 * values (0 = FALSE) indexed by tlevel
155 * int iflvl[] array of IF-ELSE-ENDIF flevel
156 * values indexed by tlevel
157 * int incfil current file handle index
159 * char * ip pointer into the assembler-source
161 * jmp_buf jump_env compiler dependent structure
162 * used by setjmp() and longjmp()
163 * int lflag -l, generate listing flag
164 * int line current assembler source
166 * int lop current line number on page
167 * int oflag -o, generate relocatable output flag
168 * int jflag -j, generate debug info flag
169 * int page current page number
170 * int pflag enable listing pagination
171 * int pass assembler pass number
172 * int radix current number conversion radix:
173 * 2 (binary), 8 (octal), 10 (decimal),
175 * int sflag -s, generate symbol table flag
176 * char srcfn[][] array of source file names
177 * int srcline[] current source file line
178 * char stb[] Subtitle string buffer
179 * sym * symp pointer to a symbol structure
180 * int tlevel current conditional level
181 * int xflag -x, listing radix flag
182 * FILE * lfp list output file handle
183 * FILE * ofp relocation output file handle
184 * FILE * tfp symbol table output file handle
185 * FILE * sfp[] array of assembler-source file handles
188 * FILE * afile() asmain.c
189 * VOID allglob() assym.c
190 * VOID asexit() asmain.c
191 * VOID diag() assubr.c
192 * VOID err() assubr.c
193 * int fprintf() c-library
194 * int as_getline() aslex.c
195 * VOID list() aslist.c
196 * VOID lstsym() aslist.c
197 * VOID minit() ___mch.c
198 * VOID newdot() asmain.c
199 * VOID outchk() asout.c
200 * VOID outgsd() asout.c
201 * int rewind() c-library
202 * int setjmp() c-library
203 * VOID symglob() assym.c
204 * VOID syminit() assym.c
205 * VOID usage() asmain.c
208 * Completion of main() completes the assembly process.
209 * REL, LST, and/or SYM files may be generated.
216 main(int argc, char *argv[])
222 /*fprintf(stdout, "\n");*/
225 for (i=1; i<argc; ++i) {
231 while ((c = *p++) != 0)
251 search_path_append(p);
256 case 'j': /* JLH: debug info */
259 ++oflag; /* force object */
306 if (++inpfil == MAXFIL) {
307 fprintf(stderr, "too many input files\n");
310 sfp[inpfil] = afile(p, "", 0);
311 strcpy(srcfn[inpfil],afn);
314 lfp = afile(p, "lst", 1);
316 ofp = afile(p, "rel", 1);
317 // save the file name if we have to delete it on error
321 tfp = afile(p, "sym", 1);
328 for (pass=0; pass<3; ++pass) {
329 if (gflag && pass == 1)
331 if (aflag && pass == 1)
333 if (oflag && pass == 2)
347 for (i = 0; i <= inpfil; i++)
360 while (as_getline()) {
366 /* JLH: if line begins with ";!", then
367 * pass this comment on to the output file
369 if (oflag && (pass == 1) &&
370 (ip[0] == ';') && (ip[1] == '!'))
372 fprintf(ofp, "%s\n", ip );
375 if (setjmp(jump_env) == 0)
383 newdot(dot.s_area); /* Flush area info */
384 if (flevel || tlevel)
388 outchk(HUGE, HUGE); /* Flush */
395 //printf ("aserr: %d\n", aserr);
396 //printf ("fatalErrors: %d\n", fatalErrors);
398 return 0; // hush the compiler
401 /*)Function VOID asexit(i)
405 * The function asexit() explicitly closes all open
406 * files and then terminates the program.
412 * FILE * ifp[] array of include-file file handles
413 * FILE * lfp list output file handle
414 * FILE * ofp relocation output file handle
415 * FILE * tfp symbol table output file handle
416 * FILE * sfp[] array of assembler-source file handles
419 * int fclose() c-library
420 * VOID exit() c-library
423 * All files closed. Program terminates.
431 if (lfp != NULL) fclose(lfp);
432 if (ofp != NULL) fclose(ofp);
433 if (tfp != NULL) fclose(tfp);
435 for (j=0; j<MAXFIL && sfp[j] != NULL; j++) {
439 /*for (j=0; j<MAXINC && ifp[j] != NULL; j++) {
443 // remove output file
444 printf ("removing %s\n", relFile);
450 /*)Function VOID asmbl()
452 * The function asmbl() scans the assembler-source text for
453 * (1) labels, global labels, equates, global equates, and local
454 * symbols, (2) .if, .else, .endif, and .page directives,
455 * (3) machine independent assembler directives, and (4) machine
456 * dependent mnemonics.
459 * mne * mp pointer to a mne structure
460 * sym * sp pointer to a sym structure
461 * tsym * tp pointer to a tsym structure
462 * int c character from assembler-source
464 * area * ap pointer to an area structure
465 * expr e1 expression structure
466 * char id[] id string
467 * char opt[] options string
468 * char fn[] filename string
469 * char * p pointer into a string
470 * int d temporary value
471 * int n temporary value
472 * int uaf user area options flag
473 * int uf area options
476 * area * areap pointer to an area structure
477 * char ctype[] array of character types, one per
479 * int flevel IF-ELSE-ENDIF flag will be non
480 * zero for false conditional case
481 * Addr_T fuzz tracks pass to pass changes in the
482 * address of symbols caused by
483 * variable length instruction formats
484 * int ifcnd[] array of IF statement condition
485 * values (0 = FALSE) indexed by tlevel
486 * int iflvl[] array of IF-ELSE-ENDIF flevel
487 * values indexed by tlevel
488 * FILE * ifp[] array of include-file file handles
489 * char incfn[][] array of include file names
490 * int incline[] current include file line
491 * int incfil current file handle index
493 * Addr_T laddr address of current assembler line
494 * or value of .if argument
495 * int lmode listing mode
496 * int lop current line number on page
497 * char module[] module name string
498 * int pass assembler pass number
499 * int radix current number conversion radix:
500 * 2 (binary), 8 (octal), 10 (decimal),
502 * char stb[] Subtitle string buffer
503 * sym * symp pointer to a symbol structure
504 * char tb[] Title string buffer
505 * int tlevel current conditional level
508 * Addr_T absexpr() asexpr.c
509 * area * alookup() assym.c
510 * VOID clrexpr() asexpr.c
511 * int digit() asexpr.c
512 * char endline() aslex.c
513 * VOID err() assubr.c
514 * VOID expr() asexpr.c
515 * FILE * fopen() c-library
517 * VOID getid() aslex.c
518 * int getmap() aslex.c
519 * char getnb() aslex.c
520 * VOID getst() aslex.c
521 * sym * lookup() assym.c
522 * VOID machine() ___mch.c
523 * mne * mlookup() assym.c
525 * VOID * new() assym.c
526 * VOID newdot() asmain.c
527 * VOID outall() asout.c
528 * VOID outab() asout.c
529 * VOID outchk() asout.c
530 * VOID outrb() asout.c
531 * VOID outrw() asout.c
532 * VOID phase() asmain.c
533 * VOID qerr() assubr.c
534 * char * strcpy() c-library
535 * char * strncpy() c-library
536 * VOID unget() aslex.c
542 register struct mne *mp;
543 register struct sym *sp;
544 register struct tsym *tp;
553 static struct area *abs_ap; /* pointer to current absolute area structure */
558 if ((c=endline()) == 0) { return; }
560 * If the first character is a digit then assume
561 * a local symbol is being specified. The symbol
562 * must end with $: to be valid.
564 * Construct a tsym structure at the first
565 * occurance of the symbol. Flag the symbol
566 * as multiply defined if not the first occurance.
568 * Load area, address, and fuzz values
569 * into structure tsym.
571 * Check for assembler phase error and
572 * multiply defined error.
574 if (ctype[c] & DIGIT) {
578 while ((d = digit(c, 10)) >= 0) {
582 if (c != '$' || get() != ':')
587 if (n == tp->t_num) {
594 tp=(struct tsym *) new (sizeof(struct tsym));
595 tp->t_lnk = symp->s_tsym;
598 tp->t_area = dot.s_area;
599 tp->t_addr = dot.s_addr;
604 if (n == tp->t_num) {
611 fuzz = tp->t_addr - dot.s_addr;
612 tp->t_area = dot.s_area;
613 tp->t_addr = dot.s_addr;
615 phase(tp->t_area, tp->t_addr);
616 if (tp->t_flg & S_MDF)
627 * If the first character is a letter then assume a label,
628 * symbol, assembler directive, or assembler mnemonic is
631 if ((ctype[c] & LETTER) == 0) {
641 * If the next character is a : then a label is being processed.
642 * A double :: defines a global label. If this is new label
643 * then create a symbol structure.
645 * Flag multiply defined labels.
647 * Load area, address, and fuzz values
648 * into structure symp.
650 * Check for assembler phase error and
651 * multiply defined error.
656 if ((c = get()) != ':') {
664 if ((symp->s_type != S_NEW) &&
665 ((symp->s_flag & S_ASG) == 0))
666 symp->s_flag |= S_MDF;
668 fuzz = symp->s_addr - dot.s_addr;
669 symp->s_type = S_USER;
670 symp->s_area = dot.s_area;
671 symp->s_addr = dot.s_addr;
673 if (symp->s_flag & S_MDF)
675 phase(symp->s_area, symp->s_addr);
678 symp->s_flag |= S_GBL;
684 * If the next character is a = then an equate is being processed.
685 * A double == defines a global equate. If this is new variable
686 * then create a symbol structure.
691 if ((c = get()) != '=') {
700 if (e1.e_flag || e1.e_base.e_ap != dot.s_area)
703 if (sp->s_type != S_NEW && (sp->s_flag & S_ASG) == 0) {
707 sp->s_area = e1.e_base.e_ap;
708 sp->s_addr = laddr = e1.e_addr;
717 lmode = flevel ? SLIST : CLIST;
718 if ((mp = mlookup(id)) == NULL) {
724 * If we have gotten this far then we have found an
725 * assembler directive or an assembler mnemonic.
727 * Check for .if, .else, .endif, and .page directives
728 * which are not controlled by the conditional flags
730 switch (mp->m_type) {
734 if (tlevel < MAXIF) {
737 iflvl[tlevel] = flevel;
750 if (++flevel > (iflvl[tlevel]+1)) {
754 if (--flevel < iflvl[tlevel]) {
763 flevel = iflvl[tlevel--];
781 * If we are not in a false state for .if/.else then
782 * process the assembler directives here.
784 switch (mp->m_type) {
788 laddr = dot.s_addr = (dot.s_addr + 1) & ~1;
794 laddr = dot.s_addr |= 1;
803 if (mp->m_type == S_BYTE) {
808 } while ((c = getnb()) == ',');
815 Addr_T val = absexpr();
816 int bit = sizeof(val)*8 - 1;
819 if (mp->m_type == S_ULEB128) {
822 impliedBit = (val & (1 << bit)) ? 1 : 0;
824 while ((bit>0) && (((val & (1 << bit)) ? 1 : 0) == impliedBit)) {
827 if (mp->m_type == S_SLEB128) {
834 outab(0x80 | (val & 0x7f));
839 } while ((c = getnb()) == ',');
845 if ((d = getnb()) == '\0')
847 while ((c = getmap(d)) >= 0)
849 if (mp->m_type == S_ASCIZ)
854 if ((d = getnb()) == '\0')
858 if ((n = getmap(d)) >= 0) {
871 dot.s_addr += e1.e_addr*mp->m_valu;
877 if ((c = getnb()) != 0) {
879 if (p < &tb[NTITL-1])
881 } while ((c = get()) != 0);
890 if ((c = getnb()) != 0) {
892 if (p < &stb[NSBTL-1])
894 } while ((c = get()) != 0);
902 getst(id, getnb()); // a module can start with a digit
907 strncpy(module, id, NCPS);
915 if ((c = getnb()) != 0) {
917 if (p < &optsdcc[NINPUT-1])
919 } while ((c = get()) != 0);
924 /*if (pass == 0) printf("optsdcc=%s\n", optsdcc);*/
932 } while ((c = getnb()) == ',');
941 if ((c = getnb()) == '(') {
945 if (mp && mp->m_type == S_ATYP) {
951 } while ((c = getnb()) == ',');
957 if ((ap = alookup(id)) != NULL) {
958 if (uaf && uf != ap->a_flag)
961 ap = (struct area *) new (sizeof(struct area));
963 strncpy(ap->a_id, id, NCPS);
964 ap->a_ref = areap->a_ref + 1;
968 ap->a_flag = uaf ? uf : (A_CON|A_REL);
973 if (dot.s_area->a_flag & A_ABS)
978 if (dot.s_area->a_flag & A_ABS) {
982 sprintf(buf, "%s%x", abs_ap->a_id, org_cnt++);
983 if ((ap = alookup(buf)) == NULL) {
984 ap = (struct area *) new (sizeof(struct area));
987 strncpy(ap->a_id, buf, NCPS);
988 ap->a_ref = areap->a_ref + 1;
995 dot.s_addr = dot.s_org = laddr;
1039 while ((c = get()) != d) {
1040 if (p < &fn[PATH_MAX-1]) {
1047 if ((++incfil == MAXINC) ||
1048 (ifp[incfil] = search_path_fopen(fn, "r")) == NULL) {
1053 incline[incfil] = 0;
1054 strcpy(incfn[incfil],fn);
1064 if (!as_strcmpi(id, "on"))
1066 /* Quick sanity check: size of
1067 * Addr_T must be at least 24 bits.
1069 if (sizeof(Addr_T) < 3)
1073 "Cannot enable Flat24 mode: "
1074 "host system must have 24 bit "
1075 "or greater integers.\n");
1082 else if (!as_strcmpi(id, "off"))
1097 printf("as8051: ds390 flat mode %sabled.\n",
1098 flat24Mode ? "en" : "dis");
1104 * If not an assembler directive then go to
1105 * the machine dependent function which handles
1106 * all the assembler mnemonics.
1110 /* if cdb information then generate the line info */
1111 if (cflag && (pass == 1))
1114 /* JLH: if -j, generate a line number symbol */
1115 if (jflag && (pass == 1))
1124 /*)Function FILE * afile(fn, ft, wf)
1126 * char * fn file specification string
1127 * char * ft file type string
1128 * int wf read(0)/write(1) flag
1130 * The function afile() opens a file for reading or writing.
1131 * (1) If the file type specification string ft
1132 * is not NULL then a file specification is
1133 * constructed with the file path\name in fn
1134 * and the extension in ft.
1135 * (2) If the file type specification string ft
1136 * is NULL then the file specification is
1137 * constructed from fn. If fn does not have
1138 * a file type then the default source file
1139 * type dsft is appended to the file specification.
1141 * afile() returns a file handle for the opened file or aborts
1142 * the assembler on an open error.
1145 * int c character value
1146 * FILE * fp filehandle for opened file
1147 * char * p1 pointer to filespec string fn
1148 * char * p2 pointer to filespec string fb
1149 * char * p3 pointer to filetype string ft
1152 * char afn[] afile() constructed filespec
1153 * char dsft[] default assembler file type string
1154 * char afn[] constructed file specification string
1157 * VOID asexit() asmain.c
1158 * FILE * fopen() c_library
1159 * int fprintf() c_library
1162 * File is opened for read or write.
1166 afile(char *fn, char *ft, int wf)
1168 register char *p2, *p3;
1176 p2 = strrchr (afn, FSEPX); // search last '.'
1178 p2 = afn + strlen (afn);
1179 if (p2 > &afn[PATH_MAX-4]) // truncate filename, if it's too long
1180 p2 = &afn[PATH_MAX-4];
1183 // choose a file-extension
1184 if (*p3 == 0) { // extension supplied?
1185 p3 = strrchr (fn, FSEPX); // no: extension in fn?
1189 p3 = dsft; // no: default extension
1192 while ((c = *p3++) != 0) { // strncpy
1193 if (p2 < &afn[PATH_MAX-1])
1198 if ((fp = fopen(afn, wf?"w":"r")) == NULL) {
1199 fprintf(stderr, "%s: cannot %s.\n", afn, wf?"create":"open");
1205 /*)Function VOID newdot(nap)
1207 * area * nap pointer to the new area structure
1209 * The function newdot():
1210 * (1) copies the current values of fuzz and the last
1211 * address into the current area referenced by dot
1212 * (2) loads dot with the pointer to the new area and
1213 * loads the fuzz and last address parameters
1214 * (3) outall() is called to flush any remaining
1215 * bufferred code from the old area to the output
1218 * area * oap pointer to old area
1221 * sym dot defined as sym[0]
1222 * Addr_T fuzz tracks pass to pass changes in the
1223 * address of symbols caused by
1224 * variable length instruction formats
1230 * Current area saved, new area loaded, buffers flushed.
1234 newdot(register struct area *nap)
1236 register struct area *oap;
1239 /* fprintf (stderr, "%s dot.s_area->a_size: %d dot.s_addr: %d\n",
1240 oap->a_id, dot.s_area->a_size, dot.s_addr); */
1242 if (oap->a_flag & A_OVR) {
1243 // the size of an overlay is the biggest size encountered
1244 if (oap->a_size < dot.s_addr) {
1245 oap->a_size = dot.s_addr;
1247 } else if (oap->a_flag & A_ABS) {
1248 oap->a_addr = dot.s_org;
1249 oap->a_size += dot.s_addr - dot.s_org;
1250 dot.s_addr = dot.s_org = 0;
1253 oap->a_size = dot.s_addr;
1255 if (nap->a_flag & A_OVR) {
1256 // a new overlay starts at 0, no fuzz
1259 } else if (nap->a_flag & A_ABS) {
1260 // a new absolute starts at org, no fuzz
1261 dot.s_addr = dot.s_org;
1264 dot.s_addr = nap->a_size;
1271 /*)Function VOID phase(ap, a)
1273 * area * ap pointer to area
1274 * Addr_T a address in area
1276 * Function phase() compares the area ap and address a
1277 * with the current area dot.s_area and address dot.s_addr
1278 * to determine if the position of the symbol has changed
1279 * between assembler passes.
1285 * sym * dot defined as sym[0]
1291 * The p error is invoked if the area and/or address
1296 phase(struct area *ap, Addr_T a)
1298 if (ap != dot.s_area || a != dot.s_addr)
1303 "Usage: [-dqxjgalopsf][ -I<dir> ] file1 [file2 file3 ...]",
1304 " d decimal listing",
1306 " x hex listing (default)",
1307 " j add line number and debug information to file", /* JLH */
1308 " g undefined symbols made global",
1309 " a all user symbols made global",
1310 " l create list output file1[LST]",
1311 " o create object output file1[REL]",
1312 " s create symbol output file1[SYM]",
1313 " c generate sdcdb debug information",
1314 " p disable listing pagination",
1315 " f flag relocatable references by ` in listing file",
1316 " ff flag relocatable references by mode in listing file",
1317 "-I<dir> Add the named directory to the include file",
1318 " search path. This option may be used more than once.",
1319 " Directories are searched in the order given.",
1324 /*)Function VOID usage()
1326 * The function usage() outputs to the stderr device the
1327 * assembler name and version and a list of valid assembler options.
1330 * char ** dp pointer to an array of
1331 * text string pointers.
1334 * char cpu[] assembler type string
1335 * char * usetxt[] array of string pointers
1338 * VOID asexit() asmain.c
1339 * int fprintf() c_library
1342 * program is terminated
1350 fprintf(stderr, "\nASxxxx Assembler %s (%s)\n\n", VERSION, cpu);
1351 for (dp = usetxt; *dp; dp++)
1352 fprintf(stderr, "%s\n", *dp);