1 /*******************************************************
3 * a56 - a DSP56001 assembler
5 * Written by Quinn C. Jensen
8 *******************************************************\
11 * Copyright (C) 1990-1994 Quinn C. Jensen
13 * Permission to use, copy, modify, distribute, and sell this software
14 * and its documentation for any purpose is hereby granted without fee,
15 * provided that the above copyright notice appear in all copies and
16 * that both that copyright notice and this permission notice appear
17 * in supporting documentation. The author makes no representations
18 * about the suitability of this software for any purpose. It is
19 * provided "as is" without express or implied warranty.
22 static char *Copyright = "Copyright (C) 1990-1994 Quinn C. Jensen";
25 * main.c - The "main" code for the assembler.
35 extern unsigned long pc;
37 BOOL binary_listing = FALSE;
38 BOOL list_includes = FALSE;
41 BOOL list_on_next = TRUE;
52 char *output_file = "a56.out";
54 char *usage = "usage: a56 [-b] [-l] [-d] [-o output-file] input-file\n";
56 while((c = getopt(argc, argv, "bldo:")) != EOF) switch (c) {
73 input_file = argv[optind++];
74 if(input_file == NULL) fatal(usage);
75 obj = open_write(output_file);
92 printf("errors=%d\n", error);
93 printf("warnings=%d\n", warning);
97 struct inc inc[MAX_NEST];
104 FILE *fp = open_read(file);
107 if(inc_p >= MAX_NEST)
108 fatal("%s: include nesting too deep\n", file);
110 inc[inc_p].file = file;
115 if(inc_p > 1 && NOT list_includes)
116 list_on_next = FALSE;
118 yyin = inc[inc_p].fp;
122 static int started = 0;
136 fclose(inc[inc_p].fp);
138 list_on = list_on_next = TRUE;
140 list_on = list_on_next = FALSE;
142 yyin = inc[inc_p].fp;
150 sym_ref(sym) /* return symbol value or UNDEF if not defined yet */
153 struct sym *sp, *find_sym();
161 yyerror("%s: undefined symbol", sym);
171 #define HASH(sym) (((sym)[0] ^ sym[1]) % HASHSIZE)
173 struct sym *symtab[HASHSIZE];
175 sym_def(sym, type, seg, i, f)
182 struct sym *sp, **stop, *find_sym();
186 pass = 2; /* what a kludge */
187 yyerror("%s: multiply defined symbol", sym);
191 stop = &symtab[HASH(sym)];
192 sp = NEW(struct sym);
195 sp->name = strsave(sym);
199 sp->n.val.i = i & 0xFFFFFF;
205 fatal("internal error 304\n");
206 if(sp->n.type != type ||
207 type == INT && sp->n.val.i != (i & 0xFFFFFF) ||
208 type == FLT && sp->n.val.f != f)
209 yyerror("%s: assembler phase error", sym);
213 struct sym *find_sym(sym)
216 struct sym *sp, **stop;
218 stop = &symtab[HASH(sym)];
219 for(sp = *stop; sp; sp = sp->next)
220 if(strcmp(sp->name, sym) == 0)
229 struct sym *sp, **stop;
234 -------------------------------------\n");
236 SSSSSSSSSSSSSSSS S XXXXXX
237 SSSSSSSSSSSSSSSS S DDDDDDDDD.DDDDDDDDDD
240 for(i = 0, stop = symtab; i < HASHSIZE; i++, stop++) {
241 for(sp = *stop; sp; sp = sp->next) {
242 if(sp->n.type == INT) {
243 printf("%16s %c %06X\n", sp->name, segs[sp->n.seg], sp->n.val.i);
244 fprintf(obj, "I %06X %s\n", sp->n.val.i, sp->name);
246 printf("%16s %c %.10f\n", sp->name, segs[sp->n.seg], sp->n.val.f);
247 fprintf(obj, "F %.10f %s\n", sp->n.val.f, sp->name);
253 char *printcode(word)
256 static char list[MAX], *lp;
262 sprintf(list, "%06X<", word);
263 for(i = 0, lp = &list[7]; i < 24; i++, lp++) {
264 *lp = word & 1 << 23 - i ? '1' : '0';
266 *++lp = i % 8 == 7 ? ' ' : ',';
271 sprintf(list, "%06X", word);
276 char *spacespace[2] = {
279 /*P:XXXX_XXXXXX(XXXX_XXXX_XXXX_XXXX_XXXX_XXXX)_*/
284 return spacespace[binary_listing ? 1 : 0] + n;
289 gencode(seg, pc, word)
292 fprintf(obj, "%c %04X %06X\n", segs[seg], pc, word & 0xFFFFFF);
311 case 'b': *bp++ = '\b'; break;
312 case 'r': *bp++ = '\r'; break;
313 case 'f': *bp++ = '\f'; break;
314 case 'n': *bp++ = '\n'; break;
315 case 't': *bp++ = '\t'; break;
316 case '\\': *bp++ = '\\'; break;
319 while(*s >= '0' && *s <= '9') {
336 #define ONE 0x4000000
341 int frac = 0, div = 1;
353 frac += (*s - '0') * scale * ONE / div;
360 return frac + scale * 4 >> 3 & 0xFFFFFF;
363 /***************** psect stuff ************************/
365 struct psect *ptop = NULL, *cur_psect = NULL;
371 for(pp = ptop; pp; pp = pp->next) {
380 printf("\nSummary of psect usage\n\n");
383 section seg base last top used avail total\n\
384 -------------------------------------------------------------------------\n");
386 SSSSSSSSSSSSSSSSSSSSSSSS X FFFF FFFF FFFF 99999 100% 99999 100% 99999
389 summarize(ptop); /* do it recursively to place back in order */
401 used = pp->pc - pp->bottom;
402 avail = pp->top - pp->pc;
403 of = pp->top - pp->bottom;
407 printf("%24.24s %c %04X %04X %04X %5d %3d%% %5d %3d%% %5d\n",
408 pp->name, segs[pp->seg], pp->bottom, pp->pc, pp->top,
409 used, of ? used * 100 / of : 0, avail, of ? avail * 100 / of : 0,
413 struct psect *find_psect(name)
418 for(pp = ptop; pp; pp = pp->next)
419 if(strcmp(pp->name, name) == 0)
436 if(seg == cur_psect->seg && pc >= cur_psect->bottom &&
437 pc <= cur_psect->top) {
448 struct psect *new_psect(name, seg, bottom, top)
451 unsigned int bottom, top;
453 struct psect *pp = find_psect(name);
456 pp = (struct psect *)alloc(sizeof *pp);
459 pp->name = strsave(name);