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