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