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