2 // RMAC - Reboot's Macro Assembler for the Atari Jaguar Console System
3 // RMAC.C - Main Application Code
4 // Copyright (C) 199x Landon Dyer, 2011 - 2016 Reboot and Friends
5 // RMAC derived from MADMAC v1.07 Written by Landon Dyer, 1986
6 // Source utilised with the kind permission of Landon Dyer
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 m6502; // 1, assembling 6502 code
29 int as68_flag; // as68 kludge mode
30 int glob_flag; // Assume undefined symbols are global
31 int lsym_flag; // Include local symbols in object file
32 int sbra_flag; // Warn about possible short branches
33 int prg_flag; // !=0, produce .PRG executable (2=symbols)
34 int legacy_flag; // Do stuff like insert code in RISC assembler
35 int obj_format; // Object format flag
36 int debug; // [1..9] Enable debugging levels
37 int err_flag; // '-e' specified
38 int err_fd; // File to write error messages to
39 int rgpu, rdsp; // Assembling Jaguar GPU or DSP code
40 int list_fd; // File to write listing to
41 int regbank; // RISC register bank
42 int segpadsize; // Segment padding size
43 int endian; // Host processor endianess (0 = LE, 1 = BE)
44 char * objfname; // Object filename pointer
45 char * firstfname; // First source filename
46 char * cmdlnexec; // Executable name, pointer to ARGV[0]
47 char * searchpath; // Search path for include files
48 char defname[] = "noname.o"; // Default output filename
49 int optim_flags[OPT_COUNT]; // Specific optimisations on/off matrix
52 // Manipulate file extension.
54 // 'name' must be large enough to hold any possible filename. If 'stripp' is
55 // nonzero, any old extension is removed. If the file does not already have an
56 // extension, 'extension' is appended to the filename.
58 char * fext(char * name, char * extension, int stripp)
62 // Find beginning of "real" name (strip off path)
63 char * beg = strrchr(name, SLASHCHAR);
68 // Clobber any old extension, if requested
71 for(s=beg; *s && *s!='.'; ++s)
77 if (strrchr(beg, '.') == NULL)
78 strcat(beg, extension);
85 // Return 'item'nth element of semicolon-seperated pathnames specified in the
86 // enviroment string 's'. Copy the pathname to 'buf'. Return 0 if the 'item'
87 // nth path doesn't exist.
89 // ['item' ranges from 0 to N-1, where N = #elements in search path]
91 int nthpath(char * env_var, int itemno, char * buf)
93 char * s = searchpath;
102 while (*s != EOS && *s++ != ';')
108 while (*s != EOS && *s != ';')
118 // Display command line help
120 void DisplayHelp(void)
123 " %s [options] srcfile\n"
126 " -? or -h Display usage information\n"
127 " -dsymbol[=value] Define symbol\n"
128 " -e[errorfile] Send error messages to file, not stdout\n"
129 " -f[format] Output object file format\n"
130 " a: ALCYON (use this for ST)\n"
131 " b: BSD (use this for Jaguar)\n"
133 " x: com/exe/xex (Atari 800)\n"
134 " -i[path] Directory to search for include files\n"
135 " -l[filename] Create an output listing file\n"
136 " -n Don't do things behind your back in RISC assembler\n"
137 " -o file Output file name\n"
138 " +o[value] Turn a specific optimisation on\n"
139 " Available optimisation values and default settings:\n"
140 " o0: Absolute long adddresses to word (on)\n"
141 " o1: move.l #x,dn/an to moveq (on)\n"
142 " o2: Word branches to short (on)\n"
143 " o3: Outer displacement 0(an) to (an) (off)\n"
144 " ~o[value] Turn a specific optimisation off\n"
145 " +oall Turn all optimisations on\n"
146 " ~oall Turn all optimisations off\n"
147 " -p Create an ST .prg (without symbols)\n"
148 " -ps Create an ST .prg (with symbols)\n"
150 " -r[size] Pad segments to boundary size specified\n"
151 " w: word (2 bytes, default alignment)\n"
152 " l: long (4 bytes)\n"
153 " p: phrase (8 bytes)\n"
154 " d: double phrase (16 bytes)\n"
155 " q: quad phrase (32 bytes)\n"
156 " -s Warn about possible short branches\n"
157 " and applied optimisations\n"
158 " -u Force referenced and undefined symbols global\n"
159 " -v Set verbose mode\n"
160 " -x Turn on debugging mode\n"
161 " -y[pagelen] Set page line length (default: 61)\n"
167 // Display version information
169 void DisplayVersion(void)
172 " _ __ _ __ ___ __ _ ___ \n"
173 "| '__| '_ ` _ \\ / _` |/ __|\n"
174 "| | | | | | | | (_| | (__ \n"
175 "|_| |_| |_| |_|\\__,_|\\___|\n"
176 "\nReboot's Macro Assembler\n"
177 "Copyright (C) 199x Landon Dyer, 2011-2017 Reboot\n"
178 "V%01i.%01i.%01i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
183 // Parse optimisation options
185 int ParseOptimization(char * optstring)
189 if (*optstring == '+')
191 else if (*optstring != '~')
194 if ((optstring[2] == 'a' || optstring[2] == 'A')
195 && (optstring[3] == 'l' || optstring[3] == 'L')
196 && (optstring[4] == 'l' || optstring[4] == 'L'))
198 memset(optim_flags, onoff, OPT_COUNT * sizeof(int));
201 else if (optstring[1] == 'o' || optstring[1] == 'O') // Turn on specific optimisation
203 int opt_no = atoi(&optstring[2]);
205 if ((opt_no >= 0) && (opt_no < OPT_COUNT))
207 optim_flags[opt_no] = onoff;
221 // Process command line arguments and do an assembly
223 int Process(int argc, char ** argv)
225 int argno; // Argument number
226 SYM * sy; // Pointer to a symbol record
227 char * s; // String pointer
228 int fd; // File descriptor
229 char fnbuf[FNSIZ]; // Filename buffer
232 errcnt = 0; // Initialise error count
233 listing = 0; // Initialise listing level
234 list_flag = 0; // Initialise listing flag
235 verb_flag = perm_verb_flag; // Initialise verbose flag
236 as68_flag = 0; // Initialise as68 kludge mode
237 glob_flag = 0; // Initialise .globl flag
238 sbra_flag = 0; // Initialise short branch flag
239 debug = 0; // Initialise debug flag
240 searchpath = NULL; // Initialise search path
241 objfname = NULL; // Initialise object filename
242 list_fname = NULL; // Initialise listing filename
243 err_fname = NULL; // Initialise error filename
244 obj_format = BSD; // Initialise object format
245 firstfname = NULL; // Initialise first filename
246 err_fd = ERROUT; // Initialise error file descriptor
247 err_flag = 0; // Initialise error flag
248 rgpu = 0; // Initialise GPU assembly flag
249 rdsp = 0; // Initialise DSP assembly flag
250 lsym_flag = 1; // Include local symbols in object file
251 regbank = BANK_N; // No RISC register bank specified
252 orgactive = 0; // Not in RISC org section
253 orgwarning = 0; // No ORG warning issued
254 segpadsize = 2; // Initialise segment padding size
255 m6502 = 0; // 6502 mode off by default
257 // Initialise modules
258 InitSymbolTable(); // Symbol table
259 InitTokenizer(); // Tokenizer
260 InitLineProcessor(); // Line processor
261 InitExpression(); // Expression analyzer
262 InitSection(); // Section manager / code generator
263 InitMark(); // Mark tape-recorder
264 InitMacro(); // Macro processor
265 InitListing(); // Listing generator
266 Init6502(); // 6502 assembler
268 // Process command line arguments and assemble source files
269 for(argno=0; argno<argc; ++argno)
271 if (*argv[argno] == '-')
273 switch (argv[argno][1])
275 case 'd': // Define symbol
277 for(s=argv[argno]+2; *s!=EOS;)
286 if (argv[argno][2] == EOS)
288 printf("-d: empty symbol\n");
293 sy = lookup(argv[argno] + 2, 0, 0);
297 sy = NewSymbol(argv[argno] + 2, LABEL, 0);
301 sy->sattr = DEFINED | EQUATED | ABS;
302 sy->svalue = (*s ? (VALUE)atoi(s) : 0);
304 case 'e': // Redirect error message output
306 err_fname = argv[argno] + 2;
308 case 'f': // -f<format>
310 switch (argv[argno][2])
313 case 'a': // -fa = Alcyon [the default]
317 case 'b': // -fb = BSD (Jaguar Recommended: 3 out 4 jaguars recommend it!)
321 case 'e': // -fe = ELF
325 case 'x': // -fx = COM/EXE/XEX
330 printf("-f: unknown object format specified\n");
335 case 'g': // Debugging flag
337 printf("Debugging flag (-g) not yet implemented\n");
339 case 'i': // Set directory search path
341 searchpath = argv[argno] + 2;
343 case 'l': // Produce listing file
345 list_fname = argv[argno] + 2;
350 case 'o': // Direct object file output
352 if (argv[argno][2] != EOS)
353 objfname = argv[argno] + 2;
358 printf("Missing argument to -o");
363 objfname = argv[argno];
367 case 'p': /* -p: generate ".PRG" executable output */
370 * -p .PRG generation w/o symbols
371 * -ps .PRG generation with symbols
373 switch (argv[argno][2])
385 printf("-p: syntax error\n");
390 // Enforce Alcyon object format - kind of silly
391 // to ask for .prg output without it!
394 case 'r': // Pad seg to requested boundary size
396 switch(argv[argno][2])
398 case 'w': case 'W': segpadsize = 2; break;
399 case 'l': case 'L': segpadsize = 4; break;
400 case 'p': case 'P': segpadsize = 8; break;
401 case 'd': case 'D': segpadsize = 16; break;
402 case 'q': case 'Q': segpadsize = 32; break;
403 default: segpadsize = 2; break; // Effective autoeven();
406 case 's': // Warn about possible short branches
410 case 'u': // Make undefined symbols .globl
414 case 'v': // Verbose flag
422 case 'x': // Turn on debugging
425 printf("~ Debugging ON\n");
427 case 'y': // -y<pagelen>
429 pagelen = atoi(argv[argno] + 2);
433 printf("-y: bad page length\n");
439 case EOS: // Input is stdin
440 if (firstfname == NULL) // Kludge first filename
441 firstfname = defname;
443 include(0, "(stdin)");
446 case 'h': // Display command line usage
453 case 'n': // Turn off legacy mode
456 printf("Legacy mode OFF\n");
460 printf("Unknown switch: %s\n\n", argv[argno]);
466 else if (*argv[argno] == '+' || *argv[argno] == '~')
468 if (ParseOptimization(argv[argno]) != OK)
471 printf("Unknown switch: %s\n\n", argv[argno]);
479 // Record first filename.
480 if (firstfname == NULL)
481 firstfname = argv[argno];
483 strcpy(fnbuf, argv[argno]);
484 fext(fnbuf, ".s", 0);
489 printf("Cannot open: %s\n", fnbuf);
499 // Wind-up processing;
500 // o save current section (no more code generation)
501 // o do auto-even of all sections (or boundary alignment as requested
503 // o save the last pc value for 6502 (if we used 6502 at all) and
504 // detemine if the last section contains anything
505 // o determine name of object file:
506 // - "foo.o" for linkable output;
507 // - "foo.prg" for GEMDOS executable (-p flag).
510 for(i=TEXT; i<=BSS; i<<=1)
516 case 2: d_even(); break;
517 case 4: d_long(); break;
518 case 8: d_phrase(); break;
519 case 16: d_dphrase(); break;
520 case 32: d_qphrase(); break;
526 SwitchSection(M6502);
527 if (sloc != currentorg[0])
529 currentorg[1] = sloc;
533 if (objfname == NULL)
535 if (firstfname == NULL)
536 firstfname = defname;
538 strcpy(fnbuf, firstfname);
539 fext(fnbuf, (prg_flag ? ".prg" : ".o"), 1);
543 // With one pass finished, go back and:
544 // (1) run through all the fixups and resolve forward references;
545 // (1.5) ensure that remaining fixups can be handled by the linker
546 // (`lo68' format, extended (postfix) format....)
547 // (2) generate the output file image and symbol table;
548 // (3) generate relocation information from left-over fixups.
549 ResolveAllFixups(); // Do all fixups
550 StopMark(); // Stop mark tape-recorder
554 if ((fd = open(objfname, _OPEN_FLAGS, _PERM_MODE)) < 0)
559 s = (prg_flag ? "executable" : "object");
560 printf("[Writing %s file: %s]\n", s, objfname);
573 printf("[Wrapping-up listing file]\n");
583 DEBUG dump_everything();
590 // Determine processor endianess
592 int GetEndianess(void)
595 char * p = (char *)&i;
605 // Application entry point
607 int main(int argc, char ** argv)
609 perm_verb_flag = 0; // Clobber "permanent" verbose flag
610 legacy_flag = 1; // Default is legacy mode on (:-P)
612 // Set legacy optimisation flags to on
613 // and everything else to off
614 memset(optim_flags, 0, OPT_COUNT * sizeof(int));
615 optim_flags[OPT_ABS_SHORT] =
616 optim_flags[OPT_MOVEL_MOVEQ] =
617 optim_flags[OPT_BSR_BCC_S] = 1;
619 cmdlnexec = argv[0]; // Obtain executable name
621 endian = GetEndianess(); // Get processor endianess
623 // If commands were passed in, process them
625 return Process(argc - 1, argv + 1);