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