]> Shamusworld >> Repos - rmac/blob - rmac.c
Improved indexed reg error
[rmac] / rmac.c
1 //
2 // RMAC - Reboot's Macro Assembler for the Atari Jaguar Console System
3 // RMAC.C - Main Application Code
4 // Copyright (C) 199x Landon Dyer, 2011 - 2016 Reboot and Friends
5 // RMAC derived from MADMAC v1.07 Written by Landon Dyer, 1986
6 // Source utilised with the kind permission of Landon Dyer
7 //
8
9 #include "rmac.h"
10 #include "error.h"
11 #include "listing.h"
12 #include "procln.h"
13 #include "token.h"
14 #include "expr.h"
15 #include "sect.h"
16 #include "mark.h"
17 #include "macro.h"
18 #include "riscasm.h"
19 #include "direct.h"
20 #include "version.h"
21 #include "debug.h"
22 #include "symbol.h"
23 #include "object.h"
24
25 int perm_verb_flag;                             // Permanently verbose, interactive mode
26 int list_flag;                                  // "-l" listing flag on command line
27 int verb_flag;                                  // Be verbose about what's going on
28 int as68_flag;                                  // as68 kludge mode
29 int glob_flag;                                  // Assume undefined symbols are global
30 int lsym_flag;                                  // Include local symbols in object file
31 int sbra_flag;                                  // Warn about possible short branches
32 int prg_flag;                                   // !=0, produce .PRG executable (2=symbols)
33 int legacy_flag;                                // Do stuff like insert code in RISC assembler
34 int obj_format;                                 // Object format flag
35 int debug;                                              // [1..9] Enable debugging levels
36 int err_flag;                                   // '-e' specified
37 int err_fd;                                             // File to write error messages to
38 int rgpu, rdsp;                                 // Assembling Jaguar GPU or DSP code
39 int list_fd;                                    // File to write listing to
40 int regbank;                                    // RISC register bank
41 int segpadsize;                                 // Segment padding size
42 int endian;                                             // Host processor endianess
43 char * objfname;                                // Object filename pointer
44 char * firstfname;                              // First source filename
45 char * cmdlnexec;                               // Executable name, pointer to ARGV[0]
46 char * searchpath;                              // Search path for include files 
47 char defname[] = "noname.o";    // Default output filename
48 int optim_flags[OPT_COUNT];     // Specific optimisations on/off matrix
49
50 //
51 // Manipulate file extension.
52 //
53 // 'name' must be large enough to hold any possible filename. If 'stripp' is
54 // nonzero, any old extension is removed. If the file does not already have an
55 // extension, 'extension' is appended to the filename.
56 //
57 char * fext(char * name, char * extension, int stripp)
58 {
59         char * s;
60
61         // Find beginning of "real" name (strip off path)
62         char * beg = strrchr(name, SLASHCHAR);
63
64         if (beg == NULL)
65                 beg = name;
66
67         // Clobber any old extension, if requested
68         if (stripp)
69         {
70                 for(s=beg; *s && *s!='.'; ++s)
71                         ;
72
73                 *s = '\0';
74         }
75
76         if (strrchr(beg, '.') == NULL)
77                 strcat(beg, extension);
78
79         return name;
80 }
81
82
83 //
84 // Return 'item'nth element of semicolon-seperated pathnames specified in the
85 // enviroment string 's'. Copy the pathname to 'buf'.  Return 0 if the 'item'
86 // nth path doesn't exist.
87 //
88 // ['item' ranges from 0 to N-1, where N = #elements in search path]
89 //
90 int nthpath(char * env_var, int itemno, char * buf)
91 {
92         char * s = searchpath;
93
94         if (s == NULL)
95                 s = getenv(env_var);
96
97         if (s == NULL)
98                 return 0;
99
100         while (itemno--)
101                 while (*s != EOS && *s++ != ';')
102                         ;
103
104         if (*s == EOS)
105                 return 0;
106
107         while (*s != EOS && *s != ';')
108                 *buf++ = *s++;
109
110         *buf++ = EOS;
111
112         return 1;
113 }
114
115
116 //
117 // Display command line help
118 //
119 void DisplayHelp(void)
120 {
121         printf("Usage:\n"
122                 "    %s [options] srcfile\n"
123                 "\n"
124                 "Options:\n"
125                 "  -? or -h          Display usage information\n"
126                 "  -dsymbol[=value]  Define symbol\n"
127                 "  -e[errorfile]     Send error messages to file, not stdout\n"
128                 "  -f[format]        Output object file format\n"
129                 "                    a: ALCYON (use this for ST)\n"
130                 "                    b: BSD (use this for Jaguar)\n"
131                 "  -i[path]          Directory to search for include files\n"
132                 "  -l[filename]      Create an output listing file\n"
133                 "  -n                Don't do things behind your back in RISC assembler\n"
134                 "  -o file           Output file name\n"
135                 "  +o[value]         Turn a specific optimisation on\n"
136                 "  ~o[value]         Turn a specific optimisation off\n"
137                 "  +oall             Turn all optimisations on\n"
138                 "  ~oall             Turn all optimisations off\n"
139                 "  -p                Create an ST .prg (without symbols)\n"
140                 "  -ps               Create an ST .prg (with symbols)\n"
141                 "                    Forces -fa\n"
142                 "  -r[size]          Pad segments to boundary size specified\n"
143                 "                    w: word (2 bytes, default alignment)\n"
144                 "                    l: long (4 bytes)\n"
145                 "                    p: phrase (8 bytes)\n"
146                 "                    d: double phrase (16 bytes)\n"
147                 "                    q: quad phrase (32 bytes)\n"
148                 "  -s                Warn about possible short branches\n"
149                 "                    and applied optimisations\n"
150                 "  -u                Force referenced and undefined symbols global\n"
151                 "  -v                Set verbose mode\n"
152                 "  -x                Turn on debugging mode\n"
153                 "  -y[pagelen]       Set page line length (default: 61)\n"
154                 "\n", cmdlnexec);
155 }
156
157
158 //
159 // Display version information
160 //
161 void DisplayVersion(void)
162 {
163         printf("\n"
164                 " _ __ _ __ ___   __ _  ___ \n"
165                 "| '__| '_ ` _ \\ / _` |/ __|\n"
166                 "| |  | | | | | | (_| | (__ \n"
167                 "|_|  |_| |_| |_|\\__,_|\\___|\n"
168                 "\nReboot's Macro Assembler\n"
169                 "Copyright (C) 199x Landon Dyer, 2011-2017 Reboot\n"
170                 "V%01i.%01i.%01i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
171 }
172
173
174 //
175 // Parse optimisation options
176 //
177 int ParseOptimization(char * optstring)
178 {
179         int onoff = 0;
180
181         if (*optstring == '+')
182                 onoff = 1;
183         else if (*optstring != '~')
184                 return ERROR;
185
186         if ((optstring[2] == 'a' || optstring[2] == 'A')
187                 && (optstring[3] == 'l' || optstring[3] == 'L')
188                 && (optstring[4] == 'l' || optstring[4] == 'L'))
189         {
190                 memset(optim_flags, onoff, OPT_COUNT * sizeof(int));
191                 return OK;
192         }
193         else if (optstring[1] == 'o' || optstring[1] == 'O') // Turn on specific optimisation
194         {
195                 int opt_no = atoi(&optstring[2]);
196
197                 if ((opt_no >= 0) && (opt_no < OPT_COUNT))
198                 {
199                         optim_flags[opt_no] = onoff;
200                         return OK;
201                 }
202                 else
203                         return ERROR;
204         }
205         else
206         {
207                 return ERROR;
208         }
209 }
210
211
212 // 
213 // Process command line arguments and do an assembly
214 //
215 int Process(int argc, char ** argv)
216 {
217         int argno;                                              // Argument number
218         SYM * sy;                                               // Pointer to a symbol record
219         char * s;                                               // String pointer
220         int fd;                                                 // File descriptor
221         char fnbuf[FNSIZ];                              // Filename buffer
222         int i;                                                  // Iterator
223
224         errcnt = 0;                                             // Initialise error count
225         listing = 0;                                    // Initialise listing level
226         list_flag = 0;                                  // Initialise listing flag
227         verb_flag = perm_verb_flag;             // Initialise verbose flag
228         as68_flag = 0;                                  // Initialise as68 kludge mode
229         glob_flag = 0;                                  // Initialise .globl flag
230         sbra_flag = 0;                                  // Initialise short branch flag
231         debug = 0;                                              // Initialise debug flag
232         searchpath = NULL;                              // Initialise search path
233         objfname = NULL;                                // Initialise object filename
234         list_fname = NULL;                              // Initialise listing filename
235         err_fname = NULL;                               // Initialise error filename
236         obj_format = BSD;                               // Initialise object format
237         firstfname = NULL;                              // Initialise first filename
238         err_fd = ERROUT;                                // Initialise error file descriptor
239         err_flag = 0;                                   // Initialise error flag
240         rgpu = 0;                                               // Initialise GPU assembly flag
241         rdsp = 0;                                               // Initialise DSP assembly flag
242         lsym_flag = 1;                                  // Include local symbols in object file
243         regbank = BANK_N;                               // No RISC register bank specified
244         orgactive = 0;                                  // Not in RISC org section
245         orgwarning = 0;                                 // No ORG warning issued
246         segpadsize = 2;                                 // Initialise segment padding size
247
248         // Initialise modules
249         InitSymbolTable();                              // Symbol table
250         InitTokenizer();                                // Tokenizer
251         InitLineProcessor();                    // Line processor
252         InitExpression();                               // Expression analyzer
253         InitSection();                                  // Section manager / code generator
254         InitMark();                                             // Mark tape-recorder
255         InitMacro();                                    // Macro processor
256         InitListing();                                  // Listing generator
257
258         // Process command line arguments and assemble source files
259         for(argno=0; argno<argc; ++argno)
260         {
261                 if (*argv[argno] == '-')
262                 {
263                         switch (argv[argno][1])
264                         {
265                         case 'd':                               // Define symbol
266                         case 'D':
267                                 for(s=argv[argno]+2; *s!=EOS;)
268                                 {
269                                         if (*s++ == '=')
270                                         {
271                                                 s[-1] = EOS;
272                                                 break;
273                                         }
274                                 }
275
276                                 if (argv[argno][2] == EOS)
277                                 {
278                                         printf("-d: empty symbol\n");
279                                         errcnt++;
280                                         return errcnt;
281                                 }
282
283                                 sy = lookup(argv[argno] + 2, 0, 0);
284
285                                 if (sy == NULL)
286                                 {
287                                         sy = NewSymbol(argv[argno] + 2, LABEL, 0);
288                                         sy->svalue = 0;
289                                 }
290
291                                 sy->sattr = DEFINED | EQUATED | ABS;
292                                 sy->svalue = (*s ? (VALUE)atoi(s) : 0);
293                                 break;
294                         case 'e':                               // Redirect error message output
295                         case 'E':
296                                 err_fname = argv[argno] + 2;
297                                 break;
298                         case 'f':                               // -f<format>
299                         case 'F':
300                                 switch (argv[argno][2])
301                                 {
302                                 case EOS:
303                                 case 'a':                       // -fa = Alcyon [the default]
304                                 case 'A':
305                                         obj_format = ALCYON;
306                                         break;
307                                 case 'b':                       // -fb = BSD (Jaguar Recommended)
308                                 case 'B':
309                                         obj_format = BSD;
310                                         break;
311                                 default:
312                                         printf("-f: unknown object format specified\n");
313                                         errcnt++;
314                                         return errcnt;
315                                 }
316                                 break;
317                         case 'g':                               // Debugging flag
318                         case 'G':
319                                 printf("Debugging flag (-g) not yet implemented\n");
320                                 break;
321                         case 'i':                               // Set directory search path
322                         case 'I':
323                                 searchpath = argv[argno] + 2;
324                                 break;
325                         case 'l':                               // Produce listing file
326                         case 'L':
327                                 list_fname = argv[argno] + 2;
328                                 listing = 1;
329                                 list_flag = 1;
330                                 lnsave++;
331                                 break;
332                         case 'o':                               // Direct object file output
333                         case 'O':
334                                 if (argv[argno][2] != EOS)
335                                         objfname = argv[argno] + 2;
336                                 else
337                                 {
338                                         if (++argno >= argc)
339                                         {
340                                                 printf("Missing argument to -o");
341                                                 errcnt++;
342                                                 return errcnt;
343                                         }
344
345                                         objfname = argv[argno];
346                                 }
347
348                                 break;
349                         case 'p':               /* -p: generate ".PRG" executable output */
350                         case 'P':
351                                 /*
352                                  * -p           .PRG generation w/o symbols
353                                  * -ps  .PRG generation with symbols
354                                  */
355                                 switch (argv[argno][2])
356                                 {
357                                         case EOS:
358                                                 prg_flag = 1;
359                                                 break;
360
361                                         case 's':
362                                         case 'S':
363                                                 prg_flag = 2;
364                                                 break;
365
366                                         default:
367                                                 printf("-p: syntax error\n");
368                                                 ++errcnt;
369                                                 return errcnt;
370                                 }
371
372                                 // Enforce Alcyon object format - kind of silly
373                                 // to ask for .prg output without it!
374                                 obj_format = ALCYON;
375                                 break;
376                         case 'r':                               // Pad seg to requested boundary size
377                         case 'R':
378                                 switch(argv[argno][2])
379                                 {
380                                 case 'w': case 'W': segpadsize = 2;  break;
381                                 case 'l': case 'L': segpadsize = 4;  break;
382                                 case 'p': case 'P': segpadsize = 8;  break;
383                                 case 'd': case 'D': segpadsize = 16; break;
384                                 case 'q': case 'Q': segpadsize = 32; break;
385                                 default: segpadsize = 2; break; // Effective autoeven();
386                                 }
387                                 break;
388                         case 's':                               // Warn about possible short branches
389                         case 'S':
390                                 sbra_flag = 1;
391                                 break;
392                         case 'u':                               // Make undefined symbols .globl
393                         case 'U':
394                                 glob_flag = 1;
395                                 break;
396                         case 'v':                               // Verbose flag
397                         case 'V':
398                                 verb_flag++;
399
400                                 if (verb_flag > 1)
401                                         DisplayVersion();
402
403                                 break;
404                         case 'x':                               // Turn on debugging
405                         case 'X':
406                                 debug = 1;
407                                 printf("~ Debugging ON\n");
408                                 break;
409                         case 'y':                               // -y<pagelen>
410                         case 'Y':
411                                 pagelen = atoi(argv[argno] + 2);
412
413                                 if (pagelen < 10)
414                                 {
415                                         printf("-y: bad page length\n");
416                                         ++errcnt;
417                                         return errcnt;
418                                 }
419
420                                 break;
421                         case EOS:                               // Input is stdin
422                                 if (firstfname == NULL) // Kludge first filename
423                                         firstfname = defname;
424
425                                 include(0, "(stdin)");
426                                 Assemble();
427                                 break;
428                         case 'h':                               // Display command line usage
429                         case 'H':
430                         case '?':
431                                 DisplayVersion();
432                                 DisplayHelp();
433                                 errcnt++;
434                                 break;
435                         case 'n':                               // Turn off legacy mode
436                         case 'N':
437                                 legacy_flag = 0;
438                                 printf("Legacy mode OFF\n");
439                                 break;
440                         default:
441                                 DisplayVersion();
442                                 printf("Unknown switch: %s\n\n", argv[argno]);
443                                 DisplayHelp();
444                                 errcnt++;
445                                 break;
446                         }
447                 }
448                 else if (*argv[argno] == '+' || *argv[argno] == '~')
449                 {
450                         if (ParseOptimization(argv[argno]) != OK)
451                         {
452                                 DisplayVersion();
453                                 printf("Unknown switch: %s\n\n", argv[argno]);
454                                 DisplayHelp();
455                                 errcnt++;
456                                 break;
457                         }
458                 }
459                 else
460                 {
461                         // Record first filename.
462                         if (firstfname == NULL)
463                                 firstfname = argv[argno];
464
465                         strcpy(fnbuf, argv[argno]);
466                         fext(fnbuf, ".s", 0);
467                         fd = open(fnbuf, 0);
468
469                         if (fd < 0)
470                         {
471                                 printf("Cannot open: %s\n", fnbuf);
472                                 errcnt++;
473                                 continue;
474                         }
475
476                         include(fd, fnbuf);
477                         Assemble();
478                 }
479         }
480
481         // Wind-up processing;
482         // o  save current section (no more code generation)
483         // o  do auto-even of all sections (or boundary alignment as requested
484         //    through '-r')
485         // o  determine name of object file:
486         //    -  "foo.o" for linkable output;
487         //    -  "foo.prg" for GEMDOS executable (-p flag).
488         SaveSection();
489
490         for(i=TEXT; i<=BSS; i<<=1)
491         {
492                 SwitchSection(i);
493
494                 switch(segpadsize)
495                 {
496                 case 2:  d_even();    break;
497                 case 4:  d_long();    break;
498                 case 8:  d_phrase();  break;
499                 case 16: d_dphrase(); break;
500                 case 32: d_qphrase(); break;
501                 }
502
503                 SaveSection();
504         }
505
506         if (objfname == NULL)
507         {
508                 if (firstfname == NULL)
509                         firstfname = defname;
510
511                 strcpy(fnbuf, firstfname);
512                 fext(fnbuf, (prg_flag ? ".prg" : ".o"), 1);
513                 objfname = fnbuf;
514         }
515
516         // With one pass finished, go back and:
517         // (1)   run through all the fixups and resolve forward references;
518         // (1.5) ensure that remaining fixups can be handled by the linker
519         //       (`lo68' format, extended (postfix) format....)
520         // (2)   generate the output file image and symbol table;
521         // (3)   generate relocation information from left-over fixups.
522         ResolveAllFixups();                                             // Do all fixups
523         StopMark();                                                             // Stop mark tape-recorder
524
525         if (errcnt == 0)
526         {
527                 if ((fd = open(objfname, _OPEN_FLAGS, _PERM_MODE)) < 0)
528                         cantcreat(objfname);
529
530                 if (verb_flag)
531                 {
532                         s = (prg_flag ? "executable" : "object");
533                         printf("[Writing %s file: %s]\n", s, objfname);
534                 }
535
536                 WriteObject(fd);
537                 close(fd);
538
539                 if (errcnt != 0)
540                         unlink(objfname);
541         }
542
543         if (list_flag)
544         {
545                 if (verb_flag)
546                         printf("[Wrapping-up listing file]\n");
547
548                 listing = 1;
549                 symtable();
550                 close(list_fd);
551         }
552
553         if (err_flag)
554                 close(err_fd);
555
556         DEBUG dump_everything();
557
558         return errcnt;
559 }
560
561
562 //
563 // Determine processor endianess
564 //
565 int GetEndianess(void)
566 {
567         int i = 1;
568         char * p = (char *)&i;
569
570         if (p[0] == 1)
571                 return 0;
572
573         return 1;
574 }
575
576
577 //
578 // Application entry point
579 //
580 int main(int argc, char ** argv)
581 {
582         perm_verb_flag = 0;                             // Clobber "permanent" verbose flag
583         legacy_flag = 1;                                // Default is legacy mode on (:-P)
584
585         // Set legacy optimisation flags to on
586         // and everything else to off
587         memset(optim_flags, 0, OPT_COUNT * sizeof(int));
588         optim_flags[OPT_ABS_SHORT] = 
589                 optim_flags[OPT_MOVEL_MOVEQ] =
590                 optim_flags[OPT_BSR_BCC_S] = 1;  
591
592         cmdlnexec = argv[0];                    // Obtain executable name
593
594         endian = GetEndianess();                // Get processor endianess
595
596         // If commands were passed in, process them
597         if (argc > 1)
598                 return Process(argc - 1, argv + 1);
599
600         DisplayVersion();
601         DisplayHelp();
602
603         return 0;
604 }
605