1 /* xa_main.c - Paul's XA51 Assembler
3 Copyright 1997,2002 Paul Stoffregen (paul at pjrc dot com)
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3, or (at your option) any
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* adapted from the osu8asm project, 1995 */
19 /* http://www.pjrc.com/tech/osu8/index.html */
22 made "relocatable" by johan.knol@iduna.nl for sdcc
24 This isn't a standalone assembler anymore. It's only purpose is to
25 create relocatable modules (that has to be processed with xa_link)
26 out of sdcc-generated .xa files
36 #define printf(x...) fprintf(stderr,x)
39 #include "xa_version.h"
40 extern void yyrestart(FILE *new_file);
44 char modulename[PATH_MAX];
45 char infilename[PATH_MAX];
46 char outfilename[PATH_MAX];
47 char listfilename[PATH_MAX];
48 char symfilename[PATH_MAX];
50 /* global variables */
52 FILE *frel, *fmem, *list_fp, *sym_fp;
55 extern char last_line_text[];
56 struct symbol *sym_list=NULL;
57 struct target *targ_list=NULL;
60 int expr_result, expr_ok, jump_dest, inst;
62 char symbol_name[1000];
63 struct area_struct area[NUM_AREAS];
66 char rel_line[2][132];
68 char *areaToString (int area) {
71 case AREA_CSEG: return "CSEG";
72 case AREA_DSEG: return "DSEG";
73 //case AREA_OSEG: return "OSEG";
74 //case AREA_ISEG: return "ISEG";
75 case AREA_BSEG: return "BSEG";
76 case AREA_XSEG: return "XSEG";
77 case AREA_XISEG: return "XISEG";
78 case AREA_XINIT: return "XINIT";
79 case AREA_GSINIT: return "GSINIT";
80 //case AREA_GSFINAL: return "GSFINAL";
81 //case AREA_HOME: return "HOME";
82 //case AREA_SSEG: return "SSEG";
87 /* "mem" is replaced by area[current_area].alloc_position */
88 /* int mem=0; */ /* mem is location in memory */
90 /* add symbols to list when we find their definition in pass #1 */
91 /* we will evaluate their values in pass #2, and figure out if */
92 /* they are branch targets betweem passes 1 and 2. Every symbol */
93 /* should appear exactly once in this list, since it can't be redefined */
95 struct symbol * build_sym_list(char *thename)
97 struct symbol *new, *p;
99 if ((p=findSymbol(thename))) {
101 fprintf (stderr, "error: symbol %s already defined\n", thename);
108 //printf(" Symbol: %s Line: %d\n", thename, lineno);
109 new = (struct symbol *) malloc(sizeof(struct symbol));
110 new->name = (char *) malloc(strlen(thename)+1);
111 strcpy(new->name, thename);
117 new->line_def = lineno - 1;
118 new->area = current_area;
119 new->mode = 'X'; // start with an external
121 if (sym_list == NULL) return (sym_list = new);
123 while (p->next != NULL) p = p->next;
128 struct symbol *findSymbol (char *thename) {
130 for (p=sym_list; p; p=p->next) {
131 if (strcasecmp(thename, p->name)==0) {
138 int assign_value(char *thename, int thevalue, char mode) {
143 if (!(strcasecmp(thename, p->name))) {
144 p->area=current_area;
152 fprintf(stderr, "Internal Error! Couldn't find symbol\n");
156 int mk_bit(char *thename, int area)
162 if (!(strcasecmp(thename, p->name))) {
169 fprintf(stderr, "Internal Error! Couldn't find symbol\n");
173 int mk_sfr(char *thename)
179 if (!(strcasecmp(thename, p->name))) {
186 fprintf(stderr, "Internal Error! Couldn't find symbol\n");
191 int mk_reg(char *thename)
197 if (!(strcasecmp(thename, p->name))) {
203 fprintf(stderr, "Internal Error! Couldn't find symbol\n");
207 int mk_global(char *thename)
213 if (!(strcasecmp(thename, p->name))) {
219 fprintf(stderr, "Internal Error! Couldn't find symbol\n");
223 int get_value(char *thename)
228 if (!(strcasecmp(thename, p->name))) {
235 fprintf(stderr, "Internal Error! Couldn't find symbol value\n");
241 /* add every branch target to this list as we find them */
242 /* ok if multiple entries of same symbol name in this list */
244 struct target * build_target_list(char *thename)
246 struct target *new, *p;
247 new = (struct target *) malloc(sizeof(struct target));
248 new->name = (char *) malloc(strlen(thename)+1);
249 strcpy(new->name, thename);
251 if (targ_list == NULL) return (targ_list = new);
253 while (p->next != NULL) p = p->next;
258 /* figure out which symbols are branch targets */
262 struct symbol *p_sym;
263 struct target *p_targ;
265 while (p_targ != NULL) {
267 while (p_sym != NULL) {
268 if (!strcasecmp(p_sym->name, p_targ->name))
272 p_targ = p_targ->next;
276 void print_symbol_table()
282 fprintf(sym_fp, "Sym in %-5s: %s\n", areaToString(p->area), p->name);
283 fprintf(sym_fp, " at: 0x%04X (%5d)", p->value, p->value);
284 fprintf(sym_fp, " Def:%s", p->isdef ? "Yes" : "No ");
285 fprintf(sym_fp, " Bit:%s", p->isbit ? "Yes" : "No ");
286 fprintf(sym_fp, " Target:%s", p->istarget ? "Yes" : "No ");
287 fprintf(sym_fp, " Line %d\n", p->line_def);
290 fprintf (sym_fp, "%-7s", "SFR");
291 } else if (p->isbit && !p->area) {
292 fprintf (sym_fp, "%-7s", "SBIT");
293 } else if (p->mode=='=') {
294 fprintf (sym_fp,"ABS ");
295 } else if (!p->isdef) {
296 fprintf (sym_fp,"EXTRN ");
298 fprintf (sym_fp, "%-7s", areaToString(p->area));
300 fprintf (sym_fp, " 0x%04x (%5d)", p->value, p->value);
301 fprintf (sym_fp, " %s", p->isdef ? "D" : "-");
302 fprintf (sym_fp, "%s", p->isbit ? "B" : "-");
303 fprintf (sym_fp, "%s", p->istarget ? "T" : "-");
304 fprintf (sym_fp, " %s\n", p->name);
310 /* check that every symbol is in the table only once */
312 void check_redefine()
314 struct symbol *p1, *p2;
319 if (!strcasecmp(p1->name, p2->name)) {
320 fprintf(stderr, "Error: symbol '%s' redefined on line %d",
321 p1->name, p2->line_def);
322 fprintf(stderr, ", first defined on line %d\n", p1->line_def);
331 int is_target(char *thename)
336 if (!strcasecmp(thename, p->name)) return (p->istarget);
342 int is_bit(char *thename)
347 if (!strcasecmp(thename, p->name)) return (p->isbit);
353 int is_reg(char *thename)
358 if (!strcasecmp(thename, p->name)) return (p->isreg);
365 struct symbol *is_def(char *thename)
370 if (!strcasecmp(thename, p->name) && p->isdef)
377 struct symbol *is_ref(char *thename) {
381 if (strcasecmp(thename, p->name)==0)
388 int is_abs(char *thename) {
392 if (strcasecmp(thename, p->name)==0)
393 return p->mode == '=';
399 /* this routine is used to dump a group of bytes to the output */
400 /* it is responsible for generating the list file and sending */
401 /* the bytes one at a time to the object code generator */
402 /* this routine is also responsible for generatine the list file */
403 /* though is it expected that the lexer has placed all the actual */
404 /* original text from the line in "last_line_text" */
406 static short last_area=-1;
410 void out(int *byte_list, int num) {
414 if (num > 0) fprintf(list_fp, "%06X: ", MEM_POS);
415 else fprintf(list_fp, "\t");
417 if (last_area!=current_area) {
418 // emit area information
419 if (area[current_area].size) {
420 fprintf (frel, "A %s size %d flags 0\n",
421 areaToString(current_area),
422 area[current_area].size);
423 if (!area[current_area].defsEmitted) {
424 for (p=sym_list; p; p=p->next) {
425 if (p->global && p->isdef && p->area==current_area) {
427 if (p->name[strlen(p->name)-1]!='$') {
429 fprintf (frel, "S %s Abs%04x\n", p->name, p->value);
431 fprintf (frel, "S %s Def%04x\n", p->name, p->value);
436 area[current_area].defsEmitted=1;
439 last_area=current_area;
441 if (current_area==AREA_CSEG ||
442 current_area==AREA_GSINIT ||
443 current_area==AREA_XINIT) {
445 for (i=0; i<num; i++) {
447 fprintf (frel, "%sT %04x", i ? "\n" : "", MEM_POS+i);
449 fprintf (frel, " %02x", byte_list[i]);
451 fprintf (frel, "\n");
452 if (rel_line[0][0]) {
453 fprintf (frel, "%s\n", rel_line[0]);
454 if (rel_line[1][0]) {
455 fprintf (frel, "%s\n", rel_line[1]);
459 for (i=0; i<num; i++) {
460 if (!first && (i % 4) == 0) fprintf(list_fp, "\t");
461 fprintf(list_fp, "%02X", byte_list[i]);
462 if ((i+1) % 4 == 0) {
463 if (first) fprintf(list_fp, "\t%s\n", last_line_text);
464 else fprintf(list_fp, "\n");
467 if (i<num-1) fprintf(list_fp, " ");
472 if (num < 3) fprintf(list_fp, "\t");
473 fprintf(list_fp, "\t%s\n", last_line_text);
475 if (num % 4) fprintf(list_fp, "\n");
484 /* add NOPs to align memory location on a valid branch target address */
488 static int nops[] = {NOP_OPCODE, NOP_OPCODE, NOP_OPCODE, NOP_OPCODE};
491 last_line_text[0] = '\0';
493 for(num=0; (MEM_POS + num) % BRANCH_SPACING; num++) {
494 sprintf (last_line_text, "\tnop\t; word allignment");
496 if (p3) out(nops, num);
500 /* print branch out of bounds error */
504 fprintf(stderr, "Error: branch out of bounds");
505 fprintf(stderr, " in line %d\n", lineno);
509 /* turn a string like "10010110b" into an int */
511 int binary2int(char *str)
513 register int i, j=1, sum=0;
515 for (i=strlen(str)-2; i >= 0; i--) {
516 sum += j * (str[i] == '1');
522 void print_usage(int);
524 void init_areas(void)
526 area[AREA_CSEG].start=area[AREA_CSEG].alloc_position = 0;
527 area[AREA_DSEG].start=area[AREA_DSEG].alloc_position = 0;
528 area[AREA_BSEG].start=area[AREA_BSEG].alloc_position = 0;
529 area[AREA_XSEG].start=area[AREA_XSEG].alloc_position = 0;
530 area[AREA_XISEG].start=area[AREA_XISEG].alloc_position = 0;
531 area[AREA_XINIT].start=area[AREA_XINIT].alloc_position = 0;
532 area[AREA_GSINIT].start=area[AREA_GSINIT].alloc_position = 0;
533 area[AREA_GSFINAL].start=area[AREA_GSFINAL].alloc_position = 0;
534 area[AREA_HOME].start=area[AREA_HOME].alloc_position = 0;
539 int i, areas=0, globals=0;
542 fprintf (frel, "SDCCXA rel, version %1.1f\n", version);
543 for (i=1; i<NUM_AREAS; i++) {
544 if ((area[i].size=area[i].alloc_position-area[i].start)) {
548 for (p=sym_list; p; p=p->next) {
551 if (p->name[strlen(p->name)-1]!='$') {
556 fprintf (frel, "H %d areas %d global symbols\n", areas, globals);
557 fprintf (frel, "M %s\n", modulename);
558 for (p=sym_list; p; p=p->next) {
560 fprintf (frel, "S %s Ref0000\n", p->name);
565 void printVersion() {
566 printf("\nPaul's XA51 Assembler\n");
567 printf("Copyright 1997,2002 Paul Stoffregen\n\n");
568 printf("This program is free software; you can redistribute it\n");
569 printf("and/or modify it under the terms of the GNU General Public\n");
570 printf("License, Version 2, published by the Free Software Foundation\n\n");
571 printf("This program is distributed in the hope that it will be useful,\n");
572 printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
573 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
576 int verbose=0, createSymbolFile=0;
578 void process_args(int argc, char **argv)
582 if (argc < 2) print_usage(1);
584 while (++i<argc && *argv[i]=='-') {
585 if (strcmp(argv[i], "--version")==0) {
589 if (strcmp(argv[i], "--help")==0) {
592 if (strcmp(argv[i], "-v")==0) {
596 if (strcmp(argv[i], "-s")==0) {
604 // only 1 source file for now
608 strcpy(infilename, argv[i]);
610 if (strncasecmp(infilename+strlen(infilename)-4, ".asm", 3)) {
611 fprintf (stderr, "unrecognized input file: \"%s\"\n", argv[i]);
615 strcpy(modulename, infilename);
616 modulename[strlen(modulename)-4] = '\0';
617 sprintf (outfilename, "%s.rel", modulename);
618 sprintf (listfilename, "%s.lst", modulename);
619 if (createSymbolFile) {
620 sprintf (symfilename, "%s.sym", modulename);
624 /* pass #1 (p1=1) find all symbol defs and branch target names */
625 /* pass #2 (p2=1) align branch targets, evaluate all symbols */
626 /* pass #3 (p3=1) produce object code */
628 int main(int argc, char **argv)
630 process_args (argc, argv);
632 yyin = fopen(infilename, "r");
634 fprintf(stderr, "Can't open file '%s'.\n", infilename);
637 frel = fopen(outfilename, "w");
639 fprintf(stderr, "Can't write file '%s'.\n", outfilename);
642 list_fp = fopen(listfilename, "w");
643 if (list_fp == NULL) {
644 fprintf(stderr, "Can't write file '%s'.\n", listfilename);
647 if (createSymbolFile) {
648 sym_fp = fopen(symfilename, "w");
649 if (sym_fp == NULL) {
650 fprintf(stderr, "Can't write file '%s'.\n", symfilename);
655 if (verbose) printf("Pass 1: Building Symbol Table:\n");
662 if (verbose) printf("Pass 2: Aligning Branch Targets:\n");
672 if (createSymbolFile) print_symbol_table();
674 if (verbose) printf("Pass 3: Generating Object Code:\n");
688 void print_usage(int fatal)
690 FILE *out = fatal ? stderr : stdout;
692 fprintf (out, "Usage: xa_asm [-s] [-v] file.xa\n");
693 fprintf (out, " -v verbose: show progress\n");
694 fprintf (out, " -s create symbol file\n");
695 fprintf (out, " --version show version/copyright info and exit\n");
696 fprintf (out, " --help show this and exit\n");
698 // some usefull options I can think of.
699 fprintf (out, " -m create map file\n");
700 fprintf (out, " -ss create symbol file sorted by symbol\n");
701 fprintf (out, " -sa create symbol file sorted by segment/address\n");
702 fprintf (out, " --no-temps supress temp symbols in map and sym file\n");
703 fprintf (out, " --code-loc=# sets the start address of the code\n");
704 fprintf (out, " --xdata-loc=# sets the start address of the external data\n");
705 fprintf (out, " --stack-loc=# sets the start address of the stack\n");