]> git.gag.com Git - fw/sdcc/blob - as/xa51/xa_main.c
Added basic support for .area and .ds directives to the xa51 assembler.
[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 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "xa_main.h"
26
27 extern void yyrestart(FILE *new_file);
28 extern void hexout(int byte, int memory_location, int end);
29 extern int yyparse();
30
31
32 /* global variables */
33
34 FILE *fhex, *fmem, *list_fp;
35 extern FILE *yyin;
36 extern char *yytext;
37 extern char last_line_text[];
38 struct symbol *sym_list=NULL;
39 struct target *targ_list=NULL;
40 int lineno=1;
41 int p1=0, p2=0, p3=0;
42 int expr_result, expr_ok, jump_dest, inst;
43 int opcode, operand;
44 char symbol_name[1000];
45 struct area_struct area[NUM_AREAS];
46 int current_area=AREA_CSEG;
47
48
49 /* "mem" is replaced by area[current_area].alloc_position */
50 /* int mem=0; */   /* mem is location in memory */
51
52 /* add symbols to list when we find their definition in pass #1 */
53 /* we will evaluate their values in pass #2, and figure out if */
54 /* they are branch targets betweem passes 1 and 2.  Every symbol */
55 /* should appear exactly once in this list, since it can't be redefined */
56
57 struct symbol * build_sym_list(char *thename)
58 {
59         struct symbol *new, *p;
60
61 /*      printf("  Symbol: %s  Line: %d\n", thename, lineno); */
62         new = (struct symbol *) malloc(sizeof(struct symbol));
63         new->name = (char *) malloc(strlen(thename)+1);
64         strcpy(new->name, thename);
65         new->value = 0;
66         new->istarget = 0;
67         new->isdef = 0;
68         new->isbit = 0;
69         new->isreg = 0;
70         new->line_def = lineno - 1;
71         new->next = NULL;
72         if (sym_list == NULL) return (sym_list = new);
73         p = sym_list;
74         while (p->next != NULL) p = p->next;
75         p->next = new;
76         return (new);
77 }
78
79 int assign_value(char *thename, int thevalue)
80 {
81         struct symbol *p;
82
83         p = sym_list;
84         while (p != NULL) {
85                 if (!(strcasecmp(thename, p->name))) {
86                         p->value = thevalue;
87                         p->isdef = 1;
88                         return (0);
89                 }
90                 p = p->next;
91         }
92         fprintf(stderr, "Internal Error!  Couldn't find symbol\n");
93         exit(1);
94 }
95
96 int mk_bit(char *thename)
97 {
98         struct symbol *p;
99
100         p = sym_list;
101         while (p != NULL) {
102                 if (!(strcasecmp(thename, p->name))) {
103                         p->isbit = 1;
104                         return (0);
105                 }
106                 p = p->next;
107         }
108         fprintf(stderr, "Internal Error!  Couldn't find symbol\n");
109         exit(1);
110 }
111
112
113 int mk_reg(char *thename)
114 {
115         struct symbol *p;
116
117         p = sym_list;
118         while (p != NULL) {
119                 if (!(strcasecmp(thename, p->name))) {
120                         p->isreg = 1;
121                         return (0);
122                 }
123                 p = p->next;
124         }
125         fprintf(stderr, "Internal Error!  Couldn't find symbol\n");
126         exit(1);
127 }
128
129
130
131 int get_value(char *thename)
132 {
133         struct symbol *p;
134         p = sym_list;
135         while (p != NULL) {
136                 if (!(strcasecmp(thename, p->name)))
137                         return (p->value);
138                 p = p->next;
139         }
140         fprintf(stderr, "Internal Error!  Couldn't find symbol value\n");
141         exit(1);
142 }
143                 
144
145
146 /* add every branch target to this list as we find them */
147 /* ok if multiple entries of same symbol name in this list */
148
149 struct target * build_target_list(char *thename)
150 {
151         struct target *new, *p;
152         new = (struct target *) malloc(sizeof(struct target));
153         new->name = (char *) malloc(strlen(thename)+1);
154         strcpy(new->name, thename);
155         new->next = NULL;
156         if (targ_list == NULL) return (targ_list = new);
157         p = targ_list;
158         while (p->next != NULL) p = p->next;
159         p->next = new;
160         return (new);
161 }
162
163 /* figure out which symbols are branch targets */
164
165 void flag_targets()
166 {
167         struct symbol *p_sym;
168         struct target *p_targ;
169         p_targ = targ_list;
170         while (p_targ != NULL) {
171                 p_sym = sym_list;
172                 while (p_sym != NULL) {
173                         if (!strcasecmp(p_sym->name, p_targ->name))
174                                 p_sym->istarget = 1;
175                         p_sym = p_sym->next;
176                 }
177                 p_targ = p_targ->next;
178         }
179 }
180
181 void print_symbol_table()
182 {
183         struct symbol *p;
184         p = sym_list;
185         while (p != NULL) {
186                 printf("Sym:%12s = %5d (%04X)  Def:", \
187                   p->name, p->value, p->value);
188                 if (p->isdef) printf("Yes"); else printf("No ");
189                 printf("  Bit:");
190                 if (p->isbit) printf("Yes"); else printf("No ");
191                 printf("  Target:");
192                 if (p->istarget) printf("Yes"); else printf("No ");
193                 printf(" Line %d\n", p->line_def);
194                 p = p->next;
195         }
196 }
197
198 /* check that every symbol is in the table only once */
199
200 void check_redefine()
201 {
202         struct symbol *p1, *p2;
203         p1 = sym_list;
204         while (p1 != NULL) {
205                 p2 = p1->next;
206                 while (p2 != NULL) {
207                         if (!strcasecmp(p1->name, p2->name)) {
208                                 fprintf(stderr, "Error: symbol '%s' redefined on line %d", p1->name, p2->line_def);
209                                 fprintf(stderr, ", first defined on line %d\n", p1->line_def);
210                         exit(1);
211                         }
212                         p2 = p2->next;
213                 }
214                 p1 = p1->next;
215         }
216 }
217
218 int is_target(char *thename)
219 {
220         struct symbol *p;
221         p = sym_list;
222         while (p != NULL) {
223                 if (!strcasecmp(thename, p->name)) return (p->istarget);
224                 p = p->next;
225         }
226         return (0);
227 }
228
229 int is_bit(char *thename)
230 {
231         struct symbol *p;
232         p = sym_list;
233         while (p != NULL) {
234                 if (!strcasecmp(thename, p->name)) return (p->isbit);
235                 p = p->next;
236         }
237         return (0);
238 }
239
240 int is_reg(char *thename)
241 {
242         struct symbol *p;
243         p = sym_list;
244         while (p != NULL) {
245                 if (!strcasecmp(thename, p->name)) return (p->isreg);
246                 p = p->next;
247         }
248         return (0);
249 }
250
251
252 int is_def(char *thename)
253 {
254         struct symbol *p;
255         p = sym_list;
256         while (p != NULL) {
257                 if (!strcasecmp(thename, p->name) && p->isdef) return(1);
258                 p = p->next;
259         }
260         return (0);
261 }
262
263 /* this routine is used to dump a group of bytes to the output */
264 /* it is responsible for generating the list file and sending */
265 /* the bytes one at a time to the object code generator */
266 /* this routine is also responsible for generatine the list file */
267 /* though is it expected that the lexer has placed all the actual */
268 /* original text from the line in "last_line_text" */
269
270 void out(int *byte_list, int num)
271 {
272         int i, first=1;
273
274         if (num > 0) fprintf(list_fp, "%06X: ", MEM_POS);
275         else fprintf(list_fp, "\t");
276
277         for (i=0; i<num; i++) {
278                 hexout(byte_list[i], MEM_POS + i, 0);
279                 if (!first && (i % 4) == 0) fprintf(list_fp, "\t");
280                 fprintf(list_fp, "%02X", byte_list[i]);
281                 if ((i+1) % 4 == 0) {
282                         if (first) fprintf(list_fp, "\t%s\n", last_line_text);
283                         else fprintf(list_fp, "\n");
284                         first = 0;
285                 } else {
286                         if (i<num-1) fprintf(list_fp, " ");
287                 }
288         }
289         if (first) {
290                 if (num < 3) fprintf(list_fp, "\t");
291                 fprintf(list_fp, "\t%s\n", last_line_text);
292         } else {
293                 if (num % 4) fprintf(list_fp, "\n");
294         }
295 }
296
297
298 /* add NOPs to align memory location on a valid branch target address */
299
300 void pad_with_nop()
301 {
302         static int nops[] = {NOP_OPCODE, NOP_OPCODE, NOP_OPCODE, NOP_OPCODE};
303         int num;
304
305         last_line_text[0] = '\0';
306
307         for(num=0; (MEM_POS + num) % BRANCH_SPACING; num++) ;
308         if (p3) out(nops, num);
309         MEM_POS += num;
310 }
311
312 /* print branch out of bounds error */
313
314 void boob_error()
315 {
316         fprintf(stderr, "Error: branch out of bounds");
317         fprintf(stderr, " in line %d\n", lineno);
318         exit(1);
319 }
320
321 /* output the jump either direction on carry */
322 /* jump_dest and MEM_POS must have the proper values */
323
324 /* 
325 void do_jump_on_carry()
326 {
327         if (p3) {
328                 operand = REL4(jump_dest, MEM_POS);
329                 if (operand < 0) {
330                         operand *= -1;
331                         operand -= 1;
332                         if (operand > 15) boob_error();
333                         out(0x20 + (operand & 15));
334                 } else {
335                         if (operand > 15) boob_error();
336                         out(0x30 + (operand & 15));
337                 }
338         }
339 }
340 */ 
341
342 /* turn a string like "10010110b" into an int */
343
344 int binary2int(char *str)
345 {
346         register int i, j=1, sum=0;
347         
348         for (i=strlen(str)-2; i >= 0; i--) {
349                 sum += j * (str[i] == '1');
350                 j *= 2;
351         }
352         return (sum);
353 }
354
355 void print_usage();
356
357
358 /* todo: someday this will allow the user to control where the */
359 /* various memory areas go, and it will take care of assigning */
360 /* positions to area which follow others (such as OSEG getting */
361 /* set just after DSEG on the 2nd and 3rd passes when we have */
362 /* leared the size needed for each segment */
363
364 void init_areas(void)
365 {
366         area[AREA_CSEG].alloc_position = 0;
367         area[AREA_DSEG].alloc_position = 0x30;
368         area[AREA_OSEG].alloc_position = 0x80;
369         area[AREA_ISEG].alloc_position = 0;
370         area[AREA_BSEG].alloc_position = 0;
371         area[AREA_XSEG].alloc_position = 0;
372         area[AREA_XISEG].alloc_position = 0;
373         area[AREA_GSINIT].alloc_position = 0;
374         area[AREA_GSFINAL].alloc_position = 0;
375         area[AREA_HOME].alloc_position = 0;
376 }
377
378
379 /* pass #1 (p1=1) find all symbol defs and branch target names */
380 /* pass #2 (p2=1) align branch targets, evaluate all symbols */
381 /* pass #3 (p3=1) produce object code */
382
383 int main(int argc, char **argv)
384 {
385         char infilename[200], outfilename[200], listfilename[200];
386
387         if (argc < 2) print_usage();
388         strcpy(infilename, argv[1]);
389         if(strlen(infilename) > 3) {
390                 if (strncasecmp(infilename+strlen(infilename)-3, ".xa", 3))
391                         strcat(infilename, ".xa");
392         } else strcat(infilename, ".xa");
393         strcpy(outfilename, infilename);
394         outfilename[strlen(outfilename)-3] = '\0';
395         strcpy(listfilename, outfilename);
396         strcat(outfilename, ".hex");
397         strcat(listfilename, ".lst");
398         yyin = fopen(infilename, "r");
399         if (yyin == NULL) {
400                 fprintf(stderr, "Can't open file '%s'.\n", infilename);
401                 exit(1);
402         }
403         fhex = fopen(outfilename, "w");
404         if (fhex == NULL) {
405                 fprintf(stderr, "Can't write file '%s'.\n", outfilename);
406                 exit(1);
407         }
408         list_fp = fopen(listfilename, "w");
409         if (list_fp == NULL) {
410                 fprintf(stderr, "Can't write file '%s'.\n", listfilename);
411                 exit(1);
412         }
413
414         /* todo: add a command line option to supress verbose messages */
415         printf("\nPaul's XA51 Assembler\n");
416         printf("Copyright 1997,2002 Paul Stoffregen\n\n");
417         printf("This program is free software; you can redistribute it\n");
418         printf("and/or modify it under the terms of the GNU General Public\n");
419         printf("License, Version 2, published by the Free Software Foundation\n\n");
420         printf("This program is distributed in the hope that it will be useful,\n");
421         printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
422         printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
423
424
425
426         printf("    Building Symbol Table:\n");
427         p1 = 1;
428         //mem = 0;
429         init_areas();
430         yyparse();
431         flag_targets();
432         print_symbol_table();
433         check_redefine();
434         p1 = 0;
435         p2 = 1;
436         rewind(yyin);
437         yyrestart(yyin);
438         lineno = 1;
439         printf("    Aligning Branch Targets:\n");
440         //mem = 0;
441         init_areas();
442         yyparse();
443         // print_symbol_table();
444         p2 = 0;
445         p3 = 1;
446         rewind(yyin);
447         yyrestart(yyin);
448         lineno = 1;
449         printf("    Generating Object Code:\n");
450         //mem = 0;
451         init_areas();
452         yyparse();
453         fclose(yyin);
454         hexout(0, 0, 1);  /* flush and close intel hex file output */
455         return 0;
456 }
457
458
459 void print_usage()
460 {
461         fprintf(stderr, "Usage: xa_asm file\n");
462         fprintf(stderr, "   or  xa_asm file.asm\n");
463         exit(1);
464 }
465