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