63152a6216633444a29a76fac3a8e1f8527ca352
[fw/sdcc] / as / xa51 / xa_main.c
1 /* Paul's XA51 Assembler, Copyright 1997,2002 Paul Stoffregen (paul@pjrc.com)
2  *
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.
6  *
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.
11
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
15  */
16
17 /* adapted from the osu8asm project, 1995 */
18 /* http://www.pjrc.com/tech/osu8/index.html */
19
20 /* 
21    made "relocatable" by johan.knol@iduna.nl for sdcc
22    
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
26 */
27
28 #define D(x) x
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <limits.h>
34
35 #define printf(x...) fprintf(stderr,x)
36
37 #include "xa_main.h"
38 #include "xa_version.h"
39 extern void yyrestart(FILE *new_file);
40 extern int yyparse();
41
42
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];
48
49 /* global variables */
50
51 FILE *frel, *fmem, *list_fp, *sym_fp;
52 extern FILE *yyin;
53 extern char *yytext;
54 extern char last_line_text[];
55 struct symbol *sym_list=NULL;
56 struct target *targ_list=NULL;
57 int lineno=1;
58 int p1=0, p2=0, p3=0;
59 int expr_result, expr_ok, jump_dest, inst;
60 int opcode;
61 char symbol_name[1000];
62 struct area_struct area[NUM_AREAS];
63 int current_area=0;
64
65 char rel_line[2][132];
66
67 char *areaToString (int area) {
68   switch (area) 
69     {
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";
82     }
83   return ("UNKNOW");
84 }
85
86 /* "mem" is replaced by area[current_area].alloc_position */
87 /* int mem=0; */   /* mem is location in memory */
88
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 */
93
94 struct symbol * build_sym_list(char *thename)
95 {
96         struct symbol *new, *p;
97
98         if ((p=findSymbol(thename))) {
99           if (p->isdef) {
100             fprintf (stderr, "error: symbol %s already defined\n", thename);
101             exit (1);
102           } else {
103             return p;
104           }
105         }
106
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);
111         new->value = 0;
112         new->istarget = 0;
113         new->isdef = 0;
114         new->isbit = 0;
115         new->isreg = 0;
116         new->line_def = lineno - 1;
117         new->area = current_area;
118         new->mode = 'X'; // start with an external
119         new->next = NULL;
120         if (sym_list == NULL) return (sym_list = new);
121         p = sym_list;
122         while (p->next != NULL) p = p->next;
123         p->next = new;
124         return (new);
125 }
126
127 struct symbol *findSymbol (char *thename) {
128   struct symbol *p;
129   for (p=sym_list; p; p=p->next) {
130     if (strcasecmp(thename, p->name)==0) {
131       return p;
132     }
133   }
134   return NULL;
135 }
136
137 int assign_value(char *thename, int thevalue, char mode) {
138   struct symbol *p;
139   
140   p = sym_list;
141   while (p != NULL) {
142     if (!(strcasecmp(thename, p->name))) {
143       p->area=current_area;
144       p->value = thevalue;
145       p->isdef = 1;
146       p->mode = mode;
147       return (0);
148     }
149     p = p->next;
150   }
151   fprintf(stderr, "Internal Error!  Couldn't find symbol\n");
152   exit(1);
153 }
154
155 int mk_bit(char *thename, int area)
156 {
157         struct symbol *p;
158
159         p = sym_list;
160         while (p != NULL) {
161                 if (!(strcasecmp(thename, p->name))) {
162                         p->isbit = 1;
163                         p->area = area;
164                         return (0);
165                 }
166                 p = p->next;
167         }
168         fprintf(stderr, "Internal Error!  Couldn't find symbol\n");
169         exit(1);
170 }
171
172 int mk_sfr(char *thename)
173 {
174         struct symbol *p;
175
176         p = sym_list;
177         while (p != NULL) {
178                 if (!(strcasecmp(thename, p->name))) {
179                         p->issfr = 1;
180                         p->area = 0;
181                         return (0);
182                 }
183                 p = p->next;
184         }
185         fprintf(stderr, "Internal Error!  Couldn't find symbol\n");
186         exit(1);
187 }
188
189
190 int mk_reg(char *thename)
191 {
192         struct symbol *p;
193
194         p = sym_list;
195         while (p != NULL) {
196                 if (!(strcasecmp(thename, p->name))) {
197                         p->isreg = 1;
198                         return (0);
199                 }
200                 p = p->next;
201         }
202         fprintf(stderr, "Internal Error!  Couldn't find symbol\n");
203         exit(1);
204 }
205
206 int mk_global(char *thename)
207 {
208   struct symbol *p;
209   
210   p = sym_list;
211   while (p != NULL) {
212     if (!(strcasecmp(thename, p->name))) {
213       p->global = 1;
214       return (0);
215     }
216     p = p->next;
217   }
218   fprintf(stderr, "Internal Error!  Couldn't find symbol\n");
219   exit(1);
220 }
221
222 int get_value(char *thename)
223 {
224   struct symbol *p;
225   p = sym_list;
226   while (p != NULL) {
227     if (!(strcasecmp(thename, p->name))) {
228       if (p->mode=='=')
229         ;//return 0;
230       return (p->value);
231     }
232     p = p->next;
233   }
234   fprintf(stderr, "Internal Error!  Couldn't find symbol value\n");
235   exit(1);
236 }
237                 
238
239
240 /* add every branch target to this list as we find them */
241 /* ok if multiple entries of same symbol name in this list */
242
243 struct target * build_target_list(char *thename)
244 {
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);
249         new->next = NULL;
250         if (targ_list == NULL) return (targ_list = new);
251         p = targ_list;
252         while (p->next != NULL) p = p->next;
253         p->next = new;
254         return (new);
255 }
256
257 /* figure out which symbols are branch targets */
258
259 void flag_targets()
260 {
261         struct symbol *p_sym;
262         struct target *p_targ;
263         p_targ = targ_list;
264         while (p_targ != NULL) {
265                 p_sym = sym_list;
266                 while (p_sym != NULL) {
267                         if (!strcasecmp(p_sym->name, p_targ->name))
268                                 p_sym->istarget = 1;
269                         p_sym = p_sym->next;
270                 }
271                 p_targ = p_targ->next;
272         }
273 }
274
275 void print_symbol_table()
276 {
277   struct symbol *p;
278   p = sym_list;
279   while (p != NULL) {
280 #if 0
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);
287 #else
288     if (p->issfr) {
289       fprintf (sym_fp, "%-5s", "SFR");
290     } else if (p->isbit && !p->area) {
291       fprintf (sym_fp, "%-5s", "SBIT");
292     } else if (p->mode=='=') {
293       fprintf (sym_fp,"ABS");
294     } else if (!p->isdef) {
295       fprintf (sym_fp,"EXTRN");
296     } else {
297       fprintf (sym_fp, "%-5s", areaToString(p->area));
298     }
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);
304 #endif
305     p = p->next;
306   }
307 }
308
309 /* check that every symbol is in the table only once */
310
311 void check_redefine()
312 {
313   struct symbol *p1, *p2;
314   p1 = sym_list;
315   while (p1 != NULL) {
316     p2 = p1->next;
317     while (p2 != NULL) {
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);
322         exit(1);
323       }
324       p2 = p2->next;
325     }
326     p1 = p1->next;
327   }
328 }
329
330 int is_target(char *thename)
331 {
332         struct symbol *p;
333         p = sym_list;
334         while (p != NULL) {
335                 if (!strcasecmp(thename, p->name)) return (p->istarget);
336                 p = p->next;
337         }
338         return (0);
339 }
340
341 int is_bit(char *thename)
342 {
343         struct symbol *p;
344         p = sym_list;
345         while (p != NULL) {
346                 if (!strcasecmp(thename, p->name)) return (p->isbit);
347                 p = p->next;
348         }
349         return (0);
350 }
351
352 int is_reg(char *thename)
353 {
354         struct symbol *p;
355         p = sym_list;
356         while (p != NULL) {
357                 if (!strcasecmp(thename, p->name)) return (p->isreg);
358                 p = p->next;
359         }
360         return (0);
361 }
362
363
364 struct symbol *is_def(char *thename)
365 {
366   struct symbol *p;
367   p = sym_list;
368   while (p != NULL) {
369     if (!strcasecmp(thename, p->name) && p->isdef) 
370       return p;
371     p = p->next;
372   }
373   return NULL;
374 }
375
376 struct symbol *is_ref(char *thename) {
377   struct symbol *p;
378   p = sym_list;
379   while (p != NULL) {
380     if (strcasecmp(thename, p->name)==0) 
381       return p;
382     p = p->next;
383   }
384   return NULL;
385 }
386
387 /* this routine is used to dump a group of bytes to the output */
388 /* it is responsible for generating the list file and sending */
389 /* the bytes one at a time to the object code generator */
390 /* this routine is also responsible for generatine the list file */
391 /* though is it expected that the lexer has placed all the actual */
392 /* original text from the line in "last_line_text" */
393
394 static short last_area=-1;
395
396 int debug=0;
397
398 void out(int *byte_list, int num) {
399   struct symbol *p;
400   int i, first=1;
401   
402   if (num > 0) fprintf(list_fp, "%06X: ", MEM_POS);
403   else fprintf(list_fp, "\t");
404   
405   if (last_area!=current_area) {
406     // emit area information
407     if (area[current_area].size) {
408       fprintf (frel, "A %s size %d flags 0\n", 
409                areaToString(current_area),
410                area[current_area].size);
411       if  (!area[current_area].defsEmitted) {
412         for (p=sym_list; p; p=p->next) {
413           if (p->global && p->isdef && p->area==current_area) {
414             // skip temp labels
415             if (p->name[strlen(p->name)-1]!='$') {
416               if (p->mode=='=') {
417                 fprintf (frel, "S %s Abs%04x\n", p->name, p->value);
418               } else {
419                 fprintf (frel, "S %s Def%04x\n", p->name, p->value);
420               }
421             }
422           }
423         }
424         area[current_area].defsEmitted=1;
425       }
426     }
427     last_area=current_area;
428   }
429   if (current_area==AREA_CSEG ||
430       current_area==AREA_GSINIT ||
431       current_area==AREA_XINIT) {
432     if (num) {
433       for (i=0; i<num; i++) {
434         if ((i%16)==0) {
435           fprintf (frel, "%sT %04x", i ? "\n" : "", MEM_POS+i);
436         }
437         fprintf (frel, " %02x", byte_list[i]);
438       }
439       fprintf (frel, "\n");
440       if (rel_line[0][0]) {
441         fprintf (frel, "%s\n", rel_line[0]);
442         if (rel_line[1][0]) {
443           fprintf (frel, "%s\n", rel_line[1]);
444         }
445       }
446     }
447     for (i=0; i<num; i++) {
448       if (!first && (i % 4) == 0) fprintf(list_fp, "\t");
449       fprintf(list_fp, "%02X", byte_list[i]);
450       if ((i+1) % 4 == 0) {
451         if (first) fprintf(list_fp, "\t%s\n", last_line_text);
452         else fprintf(list_fp, "\n");
453         first = 0;
454       } else {
455         if (i<num-1) fprintf(list_fp, " ");
456       }
457     }
458   }
459   if (first) {
460     if (num < 3) fprintf(list_fp, "\t");
461     fprintf(list_fp, "\t%s\n", last_line_text);
462   } else {
463     if (num % 4) fprintf(list_fp, "\n");
464   }
465   operand[0][0]='\0';
466   operand[1][0]='\0';
467   rel_line[0][0]='\0';
468   rel_line[1][0]='\0';
469 }
470
471
472 /* add NOPs to align memory location on a valid branch target address */
473
474 void pad_with_nop()
475 {
476   static int nops[] = {NOP_OPCODE, NOP_OPCODE, NOP_OPCODE, NOP_OPCODE};
477   int num;
478   
479   last_line_text[0] = '\0';
480   
481   for(num=0; (MEM_POS + num) % BRANCH_SPACING; num++) {
482     sprintf (last_line_text, "\tnop\t; word allignment");
483   }
484   if (p3) out(nops, num);
485   MEM_POS += num;
486 }
487
488 /* print branch out of bounds error */
489
490 void boob_error()
491 {
492         fprintf(stderr, "Error: branch out of bounds");
493         fprintf(stderr, " in line %d\n", lineno);
494         exit(1);
495 }
496
497 /* turn a string like "10010110b" into an int */
498
499 int binary2int(char *str)
500 {
501         register int i, j=1, sum=0;
502         
503         for (i=strlen(str)-2; i >= 0; i--) {
504                 sum += j * (str[i] == '1');
505                 j *= 2;
506         }
507         return (sum);
508 }
509
510 void print_usage(int);
511
512 void init_areas(void)
513 {
514   area[AREA_CSEG].start=area[AREA_CSEG].alloc_position = 0;
515   area[AREA_DSEG].start=area[AREA_DSEG].alloc_position = 0;
516   area[AREA_BSEG].start=area[AREA_BSEG].alloc_position = 0;
517   area[AREA_XSEG].start=area[AREA_XSEG].alloc_position = 0;
518   area[AREA_XISEG].start=area[AREA_XISEG].alloc_position = 0;
519   area[AREA_XINIT].start=area[AREA_XINIT].alloc_position = 0;
520   area[AREA_GSINIT].start=area[AREA_GSINIT].alloc_position = 0;
521   area[AREA_GSFINAL].start=area[AREA_GSFINAL].alloc_position = 0;
522   area[AREA_HOME].start=area[AREA_HOME].alloc_position = 0;
523 }
524
525 void relPrelude() {
526   //char buffer[132];
527   int i, areas=0, globals=0;
528   struct symbol *p;
529
530   fprintf (frel, "SDCCXA rel, version %1.1f\n", version);
531   for (i=1; i<NUM_AREAS; i++) {
532     if ((area[i].size=area[i].alloc_position-area[i].start)) {
533       areas++;
534     }
535   }
536   for (p=sym_list; p; p=p->next) {
537     if (p->isdef) {
538       // skip temp labels
539       if (p->name[strlen(p->name)-1]!='$') {
540         globals++;
541       }
542     }
543   }
544   fprintf (frel, "H %d areas %d global symbols\n", areas, globals);
545   fprintf (frel, "M %s\n", modulename);
546   for (p=sym_list; p; p=p->next) {
547     if (!p->isdef) {
548       fprintf (frel, "S %s Ref0000\n", p->name);
549     }
550   }
551 }
552
553 void printVersion() {
554   printf("\nPaul's XA51 Assembler\n");
555   printf("Copyright 1997,2002 Paul Stoffregen\n\n");
556   printf("This program is free software; you can redistribute it\n");
557   printf("and/or modify it under the terms of the GNU General Public\n");
558   printf("License, Version 2, published by the Free Software Foundation\n\n");
559   printf("This program is distributed in the hope that it will be useful,\n");
560   printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
561   printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
562 }
563
564 int verbose=0, createSymbolFile=0;
565
566 void process_args(int argc, char **argv) 
567 {
568   int i=0;
569
570   if (argc < 2) print_usage(1);
571   
572   while (++i<argc && *argv[i]=='-') {
573     if (strcmp(argv[i], "--version")==0) {
574       printVersion();
575       exit (0);
576     }
577     if (strcmp(argv[i], "--help")==0) {
578       print_usage(0);
579     }
580     if (strcmp(argv[i], "-v")==0) {
581       verbose++;
582       continue;
583     }
584     if (strcmp(argv[i], "-s")==0) {
585       createSymbolFile++;
586       continue;
587     }
588     print_usage(1);
589   }
590
591   if (i!=argc-1) {
592     // only 1 source file for now
593     print_usage(1);
594   }
595
596   strcpy(infilename, argv[i]);
597
598   if (strncasecmp(infilename+strlen(infilename)-4, ".asm", 3)) {
599     fprintf (stderr, "unrecognized input file: \"%s\"\n", argv[i]);
600     print_usage(1);
601   }
602
603   strcpy(modulename, infilename);
604   modulename[strlen(modulename)-4] = '\0';
605   sprintf (outfilename, "%s.rel", modulename);
606   sprintf (listfilename, "%s.lst", modulename);
607   if (createSymbolFile) {
608     sprintf (symfilename, "%s.sym", modulename);
609   }
610 }
611
612 /* pass #1 (p1=1) find all symbol defs and branch target names */
613 /* pass #2 (p2=1) align branch targets, evaluate all symbols */
614 /* pass #3 (p3=1) produce object code */
615
616 int main(int argc, char **argv)
617 {
618         process_args (argc, argv);
619
620         yyin = fopen(infilename, "r");
621         if (yyin == NULL) {
622                 fprintf(stderr, "Can't open file '%s'.\n", infilename);
623                 exit(1);
624         }
625         frel = fopen(outfilename, "w");
626         if (frel == NULL) {
627                 fprintf(stderr, "Can't write file '%s'.\n", outfilename);
628                 exit(1);
629         }
630         list_fp = fopen(listfilename, "w");
631         if (list_fp == NULL) {
632                 fprintf(stderr, "Can't write file '%s'.\n", listfilename);
633                 exit(1);
634         }
635         if (createSymbolFile) {
636           sym_fp = fopen(symfilename, "w");
637           if (sym_fp == NULL) {
638             fprintf(stderr, "Can't write file '%s'.\n", symfilename);
639             exit(1);
640           }
641         }
642
643         if (verbose) printf("Pass 1: Building Symbol Table:\n");
644         p1 = 1;
645         init_areas();
646         yyparse();
647         flag_targets();
648         check_redefine();
649
650         if (verbose) printf("Pass 2: Aligning Branch Targets:\n");
651         p1 = 0;
652         p2 = 1;
653         rewind(yyin);
654         yyrestart(yyin);
655         lineno = 1;
656         init_areas();
657         yyparse();
658
659         relPrelude();
660         if (createSymbolFile) print_symbol_table();
661
662         if (verbose) printf("Pass 3: Generating Object Code:\n");
663         p2 = 0;
664         p3 = 1;
665         rewind(yyin);
666         yyrestart(yyin);
667         lineno = 1;
668         init_areas();
669         yyparse();
670
671         fclose(yyin);
672         return 0;
673 }
674
675
676 void print_usage(int fatal)
677 {
678   FILE *out = fatal ? stderr : stdout;
679
680   fprintf (out, "Usage: xa_asm [-s] [-v] file.xa\n");
681   fprintf (out, "  -v            verbose: show progress\n");
682   fprintf (out, "  -s            create symbol file\n");
683   fprintf (out, "  --version     show version/copyright info and exit\n");
684   fprintf (out, "  --help        show this and exit\n");
685 #if 0
686   // some usefull options I can think of.
687   fprintf (out, "  -m            create map file\n");
688   fprintf (out, "  -ss           create symbol file sorted by symbol\n");
689   fprintf (out, "  -sa           create symbol file sorted by segment/address\n");
690   fprintf (out, "  --no-temps    supress temp symbols in map and sym file\n");
691   fprintf (out, "  --code-loc=#  sets the start address of the code\n");
692   fprintf (out, "  --xdata-loc=# sets the start address of the external data\n");
693   fprintf (out, "  --stack-loc=# sets the start address of the stack\n");
694 #endif
695   exit(fatal);
696 }
697