]> Shamusworld >> Repos - rmac/blob - rmac.c
45cd1d8f0532ff69347de1979fa1e6ed3da18f69
[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 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-2015 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         if ((opt_no >= 0) && (opt_no < OPT_COUNT))
197             optim_flags[opt_no] = onoff;
198         else
199         {
200             return ERROR;
201         }
202     }
203     else
204     {
205         return ERROR;
206     }
207 }
208
209
210 // 
211 // Process command line arguments and do an assembly
212 //
213 int Process(int argc, char ** argv)
214 {
215         int argno;                                              // Argument number
216         SYM * sy;                                               // Pointer to a symbol record
217         char * s;                                               // String pointer
218         int fd;                                                 // File descriptor
219         char fnbuf[FNSIZ];                              // Filename buffer 
220         int i;                                                  // Iterator
221
222         errcnt = 0;                                             // Initialise error count
223         listing = 0;                                    // Initialise listing level
224         list_flag = 0;                                  // Initialise listing flag
225         verb_flag = perm_verb_flag;             // Initialise verbose flag
226         as68_flag = 0;                                  // Initialise as68 kludge mode
227         glob_flag = 0;                                  // Initialise .globl flag
228         sbra_flag = 0;                                  // Initialise short branch flag
229         debug = 0;                                              // Initialise debug flag
230         searchpath = NULL;                              // Initialise search path
231         objfname = NULL;                                // Initialise object filename
232         list_fname = NULL;                              // Initialise listing filename
233         err_fname = NULL;                               // Initialise error filename
234         obj_format = BSD;                               // Initialise object format
235         firstfname = NULL;                              // Initialise first filename
236         err_fd = ERROUT;                                // Initialise error file descriptor
237         err_flag = 0;                                   // Initialise error flag
238         rgpu = 0;                                               // Initialise GPU assembly flag
239         rdsp = 0;                                               // Initialise DSP assembly flag
240         lsym_flag = 1;                                  // Include local symbols in object file
241         regbank = BANK_N;                               // No RISC register bank specified
242         orgactive = 0;                                  // Not in RISC org section
243         orgwarning = 0;                                 // No ORG warning issued
244         segpadsize = 2;                                 // Initialise segment padding size
245
246         // Initialise modules
247         InitSymbolTable();                              // Symbol table
248         InitTokenizer();                                // Tokenizer
249         InitLineProcessor();                    // Line processor
250         InitExpression();                               // Expression analyzer
251         InitSection();                                  // Section manager / code generator
252         InitMark();                                             // Mark tape-recorder
253         InitMacro();                                    // Macro processor
254         InitListing();                                  // Listing generator
255
256         // Process command line arguments and assemble source files
257         for(argno=0; argno<argc; ++argno)
258         {
259                 if (*argv[argno] == '-')
260                 {
261                         switch (argv[argno][1])
262                         {
263                         case 'd':                               // Define symbol
264                         case 'D':
265                                 for(s=argv[argno]+2; *s!=EOS;)
266                                 {
267                                         if (*s++ == '=')
268                                         {
269                                                 s[-1] = EOS;
270                                                 break;
271                                         }
272                                 }
273
274                                 if (argv[argno][2] == EOS)
275                                 {
276                                         printf("-d: empty symbol\n");
277                                         errcnt++;
278                                         return errcnt;
279                                 }
280
281                                 sy = lookup(argv[argno] + 2, 0, 0);
282
283                                 if (sy == NULL)
284                                 {
285                                         sy = NewSymbol(argv[argno] + 2, LABEL, 0);
286                                         sy->svalue = 0;
287                                 }
288
289                                 sy->sattr = DEFINED | EQUATED | ABS;
290                                 sy->svalue = (*s ? (VALUE)atoi(s) : 0);
291                                 break;
292                         case 'e':                               // Redirect error message output
293                         case 'E':
294                                 err_fname = argv[argno] + 2;
295                                 break;
296                         case 'f':                               // -f<format>
297                         case 'F':
298                                 switch (argv[argno][2])
299                                 {
300                                 case EOS:
301                                 case 'a':                       // -fa = Alcyon [the default]
302                                 case 'A':
303                                         obj_format = ALCYON;
304                                         break;
305                                 case 'b':                       // -fb = BSD (Jaguar Recommended)
306                                 case 'B':
307                                         obj_format = BSD;
308                                         break;
309                                 default:
310                                         printf("-f: unknown object format specified\n");
311                                         errcnt++;
312                                         return errcnt;
313                                 }
314                                 break;
315                         case 'g':                               // Debugging flag
316                         case 'G':
317                                 printf("Debugging flag (-g) not yet implemented\n");
318                                 break;
319                         case 'i':                               // Set directory search path
320                         case 'I':
321                                 searchpath = argv[argno] + 2;
322                                 break;
323                         case 'l':                               // Produce listing file
324                         case 'L':
325                                 list_fname = argv[argno] + 2;
326                                 listing = 1;
327                                 list_flag = 1;
328                                 lnsave++;
329                                 break;
330                         case 'o':                               // Direct object file output
331                         case 'O':
332                                 if (argv[argno][2] != EOS)
333                                         objfname = argv[argno] + 2;
334                                 else
335                                 {
336                                         if (++argno >= argc)
337                                         {
338                                                 printf("Missing argument to -o");
339                                                 errcnt++;
340                                                 return errcnt;
341                                         }
342
343                                         objfname = argv[argno];
344                                 }
345
346                                 break;
347                         case 'p':               /* -p: generate ".PRG" executable output */
348                         case 'P':
349                                 /*
350                                  * -p           .PRG generation w/o symbols
351                                  * -ps  .PRG generation with symbols
352                                  */
353                                 switch (argv[argno][2])
354                                 {
355                                         case EOS:
356                                                 prg_flag = 1;
357                                                 break;
358
359                                         case 's':
360                                         case 'S':
361                                                 prg_flag = 2;
362                                                 break;
363
364                                         default:
365                                                 printf("-p: syntax error\n");
366                                                 ++errcnt;
367                                                 return errcnt;
368                                 }
369
370                                 // Enforce Alcyon object format - kind of silly
371                                 // to ask for .prg output without it!
372                                 obj_format = ALCYON;
373                                 break;
374                         case 'r':                               // Pad seg to requested boundary size
375                         case 'R':
376                                 switch(argv[argno][2])
377                                 {
378                                 case 'w': case 'W': segpadsize = 2;  break;  
379                                 case 'l': case 'L': segpadsize = 4;  break;
380                                 case 'p': case 'P': segpadsize = 8;  break;
381                                 case 'd': case 'D': segpadsize = 16; break;
382                                 case 'q': case 'Q': segpadsize = 32; break;
383                                 default: segpadsize = 2; break; // Effective autoeven();
384                                 }
385                                 break;
386                         case 's':                               // Warn about possible short branches
387                         case 'S':
388                                 sbra_flag = 1;
389                                 break;
390                         case 'u':                               // Make undefined symbols .globl
391                         case 'U':
392                                 glob_flag = 1;
393                                 break;
394                         case 'v':                               // Verbose flag
395                         case 'V':
396                                 verb_flag++;
397
398                                 if (verb_flag > 1)
399                                         DisplayVersion();
400
401                                 break;
402                         case 'x':                               // Turn on debugging
403                         case 'X':
404                                 debug = 1;
405                                 printf("~ Debugging ON\n");
406                                 break;
407                         case 'y':                               // -y<pagelen>
408                         case 'Y':
409                                 pagelen = atoi(argv[argno] + 2);
410
411                                 if (pagelen < 10)
412                                 {
413                                         printf("-y: bad page length\n");
414                                         ++errcnt;
415                                         return errcnt;
416                                 }
417
418                                 break;
419                         case EOS:                               // Input is stdin
420                                 if (firstfname == NULL) // Kludge first filename
421                                         firstfname = defname;
422
423                                 include(0, "(stdin)");
424                                 Assemble();
425                                 break;
426                         case 'h':                               // Display command line usage
427                         case 'H':
428                         case '?':
429                                 DisplayVersion();
430                                 DisplayHelp();
431                                 errcnt++;
432                                 break;
433                         case 'n':                               // Turn off legacy mode
434                         case 'N':
435                                 legacy_flag = 0;
436                                 printf("Legacy mode OFF\n");
437                                 break;
438                         default:
439                                 DisplayVersion();
440                                 printf("Unknown switch: %s\n\n", argv[argno]);
441                                 DisplayHelp();
442                                 errcnt++;
443                                 break;
444                         }
445                 }
446                 else if (*argv[argno] == '+' || *argv[argno] == '~')
447                 {
448                         if (ParseOptimization(argv[argno]) != OK)
449                         {
450                                 DisplayVersion();
451                                 printf("Unknown switch: %s\n\n", argv[argno]);
452                                 DisplayHelp();
453                                 errcnt++;
454                                 break;
455                         }
456                 }
457                 else
458                 {
459                         // Record first filename.
460                         if (firstfname == NULL)
461                                 firstfname = argv[argno];
462
463                         strcpy(fnbuf, argv[argno]);
464                         fext(fnbuf, ".s", 0);
465                         fd = open(fnbuf, 0);
466
467                         if (fd < 0)
468                         {
469                                 printf("Cannot open: %s\n", fnbuf);
470                                 errcnt++;
471                                 continue;
472                         }
473
474                         include(fd, fnbuf);
475                         Assemble();
476                 }
477         }
478
479         // Wind-up processing;
480         // o  save current section (no more code generation)
481         // o  do auto-even of all sections (or boundary alignment as requested
482         //    through '-r')
483         // o  determine name of object file:
484         //    -  "foo.o" for linkable output;
485         //    -  "foo.prg" for GEMDOS executable (-p flag).
486         SaveSection();
487
488         for(i=TEXT; i<=BSS; i<<=1)
489         {
490                 SwitchSection(i);
491
492                 switch(segpadsize)
493                 {
494                 case 2:  d_even();    break;
495                 case 4:  d_long();    break;
496                 case 8:  d_phrase();  break;
497                 case 16: d_dphrase(); break;
498                 case 32: d_qphrase(); break;
499                 }
500
501                 SaveSection();
502         }
503
504         if (objfname == NULL)
505         {
506                 if (firstfname == NULL)
507                         firstfname = defname;
508
509                 strcpy(fnbuf, firstfname);
510                 fext(fnbuf, (prg_flag ? ".prg" : ".o"), 1);
511                 objfname = fnbuf;
512         }
513
514         // With one pass finished, go back and:
515         // (1)   run through all the fixups and resolve forward references;
516         // (1.5) ensure that remaining fixups can be handled by the linker
517         //       (`lo68' format, extended (postfix) format....)
518         // (2)   generate the output file image and symbol table;
519         // (3)   generate relocation information from left-over fixups.
520         ResolveAllFixups();                                             // Do all fixups
521         StopMark();                                                             // Stop mark tape-recorder
522
523         if (errcnt == 0)
524         {
525                 if ((fd = open(objfname, _OPEN_FLAGS, _PERM_MODE)) < 0)
526                         cantcreat(objfname);
527
528                 if (verb_flag)
529                 {
530                         s = (prg_flag ? "executable" : "object");
531                         printf("[Writing %s file: %s]\n", s, objfname);
532                 }
533
534                 WriteObject(fd);
535                 close(fd);
536
537                 if (errcnt != 0)
538                         unlink(objfname);
539         }
540
541         if (list_flag)
542         {
543                 if (verb_flag)
544                         printf("[Wrapping-up listing file]\n");
545
546                 listing = 1;
547                 symtable();
548                 close(list_fd);
549         }
550
551         if (err_flag)
552                 close(err_fd);
553
554         DEBUG dump_everything();
555
556         return errcnt;
557 }
558
559
560 //
561 // Determine processor endianess
562 //
563 int GetEndianess(void)
564 {
565         int i = 1;
566         char * p = (char *)&i;
567
568         if (p[0] == 1)
569                 return 0;
570
571         return 1;
572 }
573
574
575 //
576 // Application entry point
577 //
578 int main(int argc, char ** argv)
579 {
580         perm_verb_flag = 0;                             // Clobber "permanent" verbose flag
581         legacy_flag = 1;                                // Default is legacy mode on (:-P)
582
583         // Set legacy optimisation flags to on
584         // and everything else to off
585         memset(optim_flags, 0, OPT_COUNT * sizeof(int));
586         optim_flags[OPT_ABS_SHORT] = 
587                 optim_flags[OPT_MOVEL_MOVEQ] =
588                 optim_flags[OPT_BSR_BCC_S] = 1;  
589
590         cmdlnexec = argv[0];                    // Obtain executable name
591
592         endian = GetEndianess();                // Get processor endianess
593
594         // If commands were passed in, process them
595         if (argc > 1)
596                 return Process(argc - 1, argv + 1);              
597
598         DisplayVersion();
599         DisplayHelp();
600
601         return 0;
602 }
603