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