1 /* Paul's XA51 Assembler, Copyright 1997,2002 Paul Stoffregen (paul@pjrc.com)
3 * Paul's XA51 Assembler is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; version 2.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 /* adapted from the osu8asm project, 1995 */
18 /* http://www.pjrc.com/tech/osu8/index.html */
21 made "relocatable" by johan.knol@iduna.nl for sdcc
23 This isn't a standalone assembler anymore. It's only purpose is to
24 create relocatable modules (that has to be processed with xa_link)
25 out of sdcc-generated .xa files
35 #define printf(x...) fprintf(stderr,x)
38 #include "xa_version.h"
39 extern void yyrestart(FILE *new_file);
43 char modulename[PATH_MAX];
44 char infilename[PATH_MAX];
45 char outfilename[PATH_MAX];
46 char listfilename[PATH_MAX];
47 char symfilename[PATH_MAX];
49 /* global variables */
51 FILE *frel, *fmem, *list_fp, *sym_fp;
54 extern char last_line_text[];
55 struct symbol *sym_list=NULL;
56 struct target *targ_list=NULL;
59 int expr_result, expr_ok, jump_dest, inst;
61 char symbol_name[1000];
62 struct area_struct area[NUM_AREAS];
65 char rel_line[2][132];
67 char *areaToString (int area) {
70 case AREA_CSEG: return "CSEG";
71 case AREA_DSEG: return "DSEG";
72 //case AREA_OSEG: return "OSEG";
73 //case AREA_ISEG: return "ISEG";
74 case AREA_BSEG: return "BSEG";
75 case AREA_XSEG: return "XSEG";
76 case AREA_XISEG: return "XISEG";
77 case AREA_XINIT: return "XINIT";
78 case AREA_GSINIT: return "GSINIT";
79 //case AREA_GSFINAL: return "GSFINAL";
80 //case AREA_HOME: return "HOME";
81 //case AREA_SSEG: return "SSEG";
86 /* "mem" is replaced by area[current_area].alloc_position */
87 /* int mem=0; */ /* mem is location in memory */
89 /* add symbols to list when we find their definition in pass #1 */
90 /* we will evaluate their values in pass #2, and figure out if */
91 /* they are branch targets betweem passes 1 and 2. Every symbol */
92 /* should appear exactly once in this list, since it can't be redefined */
94 struct symbol * build_sym_list(char *thename)
96 struct symbol *new, *p;
98 if ((p=findSymbol(thename))) {
100 fprintf (stderr, "error: symbol %s already defined\n", thename);
107 //printf(" Symbol: %s Line: %d\n", thename, lineno);
108 new = (struct symbol *) malloc(sizeof(struct symbol));
109 new->name = (char *) malloc(strlen(thename)+1);
110 strcpy(new->name, thename);
116 new->line_def = lineno - 1;
117 new->area = current_area;
118 new->mode = 'X'; // start with an external
120 if (sym_list == NULL) return (sym_list = new);
122 while (p->next != NULL) p = p->next;
127 struct symbol *findSymbol (char *thename) {
129 for (p=sym_list; p; p=p->next) {
130 if (strcasecmp(thename, p->name)==0) {
137 int assign_value(char *thename, int thevalue, char mode) {
142 if (!(strcasecmp(thename, p->name))) {
143 p->area=current_area;
151 fprintf(stderr, "Internal Error! Couldn't find symbol\n");
155 int mk_bit(char *thename, int area)
161 if (!(strcasecmp(thename, p->name))) {
168 fprintf(stderr, "Internal Error! Couldn't find symbol\n");
172 int mk_sfr(char *thename)
178 if (!(strcasecmp(thename, p->name))) {
185 fprintf(stderr, "Internal Error! Couldn't find symbol\n");
190 int mk_reg(char *thename)
196 if (!(strcasecmp(thename, p->name))) {
202 fprintf(stderr, "Internal Error! Couldn't find symbol\n");
206 int mk_global(char *thename)
212 if (!(strcasecmp(thename, p->name))) {
218 fprintf(stderr, "Internal Error! Couldn't find symbol\n");
222 int get_value(char *thename)
227 if (!(strcasecmp(thename, p->name))) {
234 fprintf(stderr, "Internal Error! Couldn't find symbol value\n");
240 /* add every branch target to this list as we find them */
241 /* ok if multiple entries of same symbol name in this list */
243 struct target * build_target_list(char *thename)
245 struct target *new, *p;
246 new = (struct target *) malloc(sizeof(struct target));
247 new->name = (char *) malloc(strlen(thename)+1);
248 strcpy(new->name, thename);
250 if (targ_list == NULL) return (targ_list = new);
252 while (p->next != NULL) p = p->next;
257 /* figure out which symbols are branch targets */
261 struct symbol *p_sym;
262 struct target *p_targ;
264 while (p_targ != NULL) {
266 while (p_sym != NULL) {
267 if (!strcasecmp(p_sym->name, p_targ->name))
271 p_targ = p_targ->next;
275 void print_symbol_table()
281 fprintf(sym_fp, "Sym in %-5s: %s\n", areaToString(p->area), p->name);
282 fprintf(sym_fp, " at: 0x%04X (%5d)", p->value, p->value);
283 fprintf(sym_fp, " Def:%s", p->isdef ? "Yes" : "No ");
284 fprintf(sym_fp, " Bit:%s", p->isbit ? "Yes" : "No ");
285 fprintf(sym_fp, " Target:%s", p->istarget ? "Yes" : "No ");
286 fprintf(sym_fp, " Line %d\n", p->line_def);
289 fprintf (sym_fp, "%-7s", "SFR");
290 } else if (p->isbit && !p->area) {
291 fprintf (sym_fp, "%-7s", "SBIT");
292 } else if (p->mode=='=') {
293 fprintf (sym_fp,"ABS ");
294 } else if (!p->isdef) {
295 fprintf (sym_fp,"EXTRN ");
297 fprintf (sym_fp, "%-7s", areaToString(p->area));
299 fprintf (sym_fp, " 0x%04x (%5d)", p->value, p->value);
300 fprintf (sym_fp, " %s", p->isdef ? "D" : "-");
301 fprintf (sym_fp, "%s", p->isbit ? "B" : "-");
302 fprintf (sym_fp, "%s", p->istarget ? "T" : "-");
303 fprintf (sym_fp, " %s\n", p->name);
309 /* check that every symbol is in the table only once */
311 void check_redefine()
313 struct symbol *p1, *p2;
318 if (!strcasecmp(p1->name, p2->name)) {
319 fprintf(stderr, "Error: symbol '%s' redefined on line %d",
320 p1->name, p2->line_def);
321 fprintf(stderr, ", first defined on line %d\n", p1->line_def);
330 int is_target(char *thename)
335 if (!strcasecmp(thename, p->name)) return (p->istarget);
341 int is_bit(char *thename)
346 if (!strcasecmp(thename, p->name)) return (p->isbit);
352 int is_reg(char *thename)
357 if (!strcasecmp(thename, p->name)) return (p->isreg);
364 struct symbol *is_def(char *thename)
369 if (!strcasecmp(thename, p->name) && p->isdef)
376 struct symbol *is_ref(char *thename) {
380 if (strcasecmp(thename, p->name)==0)
387 int is_abs(char *thename) {
391 if (strcasecmp(thename, p->name)==0)
392 return p->mode == '=';
398 /* this routine is used to dump a group of bytes to the output */
399 /* it is responsible for generating the list file and sending */
400 /* the bytes one at a time to the object code generator */
401 /* this routine is also responsible for generatine the list file */
402 /* though is it expected that the lexer has placed all the actual */
403 /* original text from the line in "last_line_text" */
405 static short last_area=-1;
409 void out(int *byte_list, int num) {
413 if (num > 0) fprintf(list_fp, "%06X: ", MEM_POS);
414 else fprintf(list_fp, "\t");
416 if (last_area!=current_area) {
417 // emit area information
418 if (area[current_area].size) {
419 fprintf (frel, "A %s size %d flags 0\n",
420 areaToString(current_area),
421 area[current_area].size);
422 if (!area[current_area].defsEmitted) {
423 for (p=sym_list; p; p=p->next) {
424 if (p->global && p->isdef && p->area==current_area) {
426 if (p->name[strlen(p->name)-1]!='$') {
428 fprintf (frel, "S %s Abs%04x\n", p->name, p->value);
430 fprintf (frel, "S %s Def%04x\n", p->name, p->value);
435 area[current_area].defsEmitted=1;
438 last_area=current_area;
440 if (current_area==AREA_CSEG ||
441 current_area==AREA_GSINIT ||
442 current_area==AREA_XINIT) {
444 for (i=0; i<num; i++) {
446 fprintf (frel, "%sT %04x", i ? "\n" : "", MEM_POS+i);
448 fprintf (frel, " %02x", byte_list[i]);
450 fprintf (frel, "\n");
451 if (rel_line[0][0]) {
452 fprintf (frel, "%s\n", rel_line[0]);
453 if (rel_line[1][0]) {
454 fprintf (frel, "%s\n", rel_line[1]);
458 for (i=0; i<num; i++) {
459 if (!first && (i % 4) == 0) fprintf(list_fp, "\t");
460 fprintf(list_fp, "%02X", byte_list[i]);
461 if ((i+1) % 4 == 0) {
462 if (first) fprintf(list_fp, "\t%s\n", last_line_text);
463 else fprintf(list_fp, "\n");
466 if (i<num-1) fprintf(list_fp, " ");
471 if (num < 3) fprintf(list_fp, "\t");
472 fprintf(list_fp, "\t%s\n", last_line_text);
474 if (num % 4) fprintf(list_fp, "\n");
483 /* add NOPs to align memory location on a valid branch target address */
487 static int nops[] = {NOP_OPCODE, NOP_OPCODE, NOP_OPCODE, NOP_OPCODE};
490 last_line_text[0] = '\0';
492 for(num=0; (MEM_POS + num) % BRANCH_SPACING; num++) {
493 sprintf (last_line_text, "\tnop\t; word allignment");
495 if (p3) out(nops, num);
499 /* print branch out of bounds error */
503 fprintf(stderr, "Error: branch out of bounds");
504 fprintf(stderr, " in line %d\n", lineno);
508 /* turn a string like "10010110b" into an int */
510 int binary2int(char *str)
512 register int i, j=1, sum=0;
514 for (i=strlen(str)-2; i >= 0; i--) {
515 sum += j * (str[i] == '1');
521 void print_usage(int);
523 void init_areas(void)
525 area[AREA_CSEG].start=area[AREA_CSEG].alloc_position = 0;
526 area[AREA_DSEG].start=area[AREA_DSEG].alloc_position = 0;
527 area[AREA_BSEG].start=area[AREA_BSEG].alloc_position = 0;
528 area[AREA_XSEG].start=area[AREA_XSEG].alloc_position = 0;
529 area[AREA_XISEG].start=area[AREA_XISEG].alloc_position = 0;
530 area[AREA_XINIT].start=area[AREA_XINIT].alloc_position = 0;
531 area[AREA_GSINIT].start=area[AREA_GSINIT].alloc_position = 0;
532 area[AREA_GSFINAL].start=area[AREA_GSFINAL].alloc_position = 0;
533 area[AREA_HOME].start=area[AREA_HOME].alloc_position = 0;
538 int i, areas=0, globals=0;
541 fprintf (frel, "SDCCXA rel, version %1.1f\n", version);
542 for (i=1; i<NUM_AREAS; i++) {
543 if ((area[i].size=area[i].alloc_position-area[i].start)) {
547 for (p=sym_list; p; p=p->next) {
550 if (p->name[strlen(p->name)-1]!='$') {
555 fprintf (frel, "H %d areas %d global symbols\n", areas, globals);
556 fprintf (frel, "M %s\n", modulename);
557 for (p=sym_list; p; p=p->next) {
559 fprintf (frel, "S %s Ref0000\n", p->name);
564 void printVersion() {
565 printf("\nPaul's XA51 Assembler\n");
566 printf("Copyright 1997,2002 Paul Stoffregen\n\n");
567 printf("This program is free software; you can redistribute it\n");
568 printf("and/or modify it under the terms of the GNU General Public\n");
569 printf("License, Version 2, published by the Free Software Foundation\n\n");
570 printf("This program is distributed in the hope that it will be useful,\n");
571 printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
572 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
575 int verbose=0, createSymbolFile=0;
577 void process_args(int argc, char **argv)
581 if (argc < 2) print_usage(1);
583 while (++i<argc && *argv[i]=='-') {
584 if (strcmp(argv[i], "--version")==0) {
588 if (strcmp(argv[i], "--help")==0) {
591 if (strcmp(argv[i], "-v")==0) {
595 if (strcmp(argv[i], "-s")==0) {
603 // only 1 source file for now
607 strcpy(infilename, argv[i]);
609 if (strncasecmp(infilename+strlen(infilename)-4, ".asm", 3)) {
610 fprintf (stderr, "unrecognized input file: \"%s\"\n", argv[i]);
614 strcpy(modulename, infilename);
615 modulename[strlen(modulename)-4] = '\0';
616 sprintf (outfilename, "%s.rel", modulename);
617 sprintf (listfilename, "%s.lst", modulename);
618 if (createSymbolFile) {
619 sprintf (symfilename, "%s.sym", modulename);
623 /* pass #1 (p1=1) find all symbol defs and branch target names */
624 /* pass #2 (p2=1) align branch targets, evaluate all symbols */
625 /* pass #3 (p3=1) produce object code */
627 int main(int argc, char **argv)
629 process_args (argc, argv);
631 yyin = fopen(infilename, "r");
633 fprintf(stderr, "Can't open file '%s'.\n", infilename);
636 frel = fopen(outfilename, "w");
638 fprintf(stderr, "Can't write file '%s'.\n", outfilename);
641 list_fp = fopen(listfilename, "w");
642 if (list_fp == NULL) {
643 fprintf(stderr, "Can't write file '%s'.\n", listfilename);
646 if (createSymbolFile) {
647 sym_fp = fopen(symfilename, "w");
648 if (sym_fp == NULL) {
649 fprintf(stderr, "Can't write file '%s'.\n", symfilename);
654 if (verbose) printf("Pass 1: Building Symbol Table:\n");
661 if (verbose) printf("Pass 2: Aligning Branch Targets:\n");
671 if (createSymbolFile) print_symbol_table();
673 if (verbose) printf("Pass 3: Generating Object Code:\n");
687 void print_usage(int fatal)
689 FILE *out = fatal ? stderr : stdout;
691 fprintf (out, "Usage: xa_asm [-s] [-v] file.xa\n");
692 fprintf (out, " -v verbose: show progress\n");
693 fprintf (out, " -s create symbol file\n");
694 fprintf (out, " --version show version/copyright info and exit\n");
695 fprintf (out, " --help show this and exit\n");
697 // some usefull options I can think of.
698 fprintf (out, " -m create map file\n");
699 fprintf (out, " -ss create symbol file sorted by symbol\n");
700 fprintf (out, " -sa create symbol file sorted by segment/address\n");
701 fprintf (out, " --no-temps supress temp symbols in map and sym file\n");
702 fprintf (out, " --code-loc=# sets the start address of the code\n");
703 fprintf (out, " --xdata-loc=# sets the start address of the external data\n");
704 fprintf (out, " --stack-loc=# sets the start address of the stack\n");